npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-autorun

v0.0.4

Published

A macro that compiles into a dependencies array for hooks.

Downloads

3

Readme

React Autorun

React Autorun is a powerful macro that simplifies the management of dependencies for hooks in React. It offers a seamless way to specify dependencies while providing control over their behavior. With React Autorun, you can easily ignore specific objects from being included as dependencies, eliminating the need for workarounds like wrapping values with useRef.

Key Features

  • Automatic Dependency Tracking: React Autorun eliminates the need to manually specify dependencies by automatically generating the dependencies array for your hooks at compile-time.
  • Flexible Ignoring of Values: You can mark certain values as ignored, ensuring they are not considered as dependencies during runtime.
  • Works with Any Hook: React Autorun decouples the dependencies' logic from the hook type, allowing you to specify dependencies for any hook, not just React's hooks.

Before and After Compilation

Before:

import { useEffect, useState } from 'react';
import { autorun } from 'react-autorun';
import { GameContext } from '../game/context';
import { createGame } from '../game/game';
import { GameBoard } from './game_board';

export function Blackjack() {
  const [game, setGame] = useState(createGame);

  useEffect(() => {
    const unlistenToRestart = game.restartEvent.listen(() => {
      setGame(createGame());
    });

    return () => {
      unlistenToRestart();
    };
  }, autorun);

  return (
    <GameContext.Provider value={game}>
      <GameBoard />
    </GameContext.Provider>
  );
}

After:

import { useEffect, useState } from 'react';
import { autorun } from 'react-autorun';
import { GameContext } from '../game/context';
import { createGame } from '../game/game';
import { GameBoard } from './game_board';

export function Blackjack() {
  const [game, setGame] = useState(createGame);

  useEffect(() => {
    const unlistenToRestart = game.restartEvent.listen(() => {
      setGame(createGame());
    });

    return () => {
      unlistenToRestart();
    };
  }, autorun(() => [game?.restartEvent, game?.restartEvent?.listen]));

  return (
    <GameContext.Provider value={game}>
      <GameBoard />
    </GameContext.Provider>
  );
}

The "after" code showcases how React Autorun can transform the dependencies array for hooks, providing a more streamlined and intuitive approach.

Ignoring Values with autorun.ignore

React Autorun provides a way to ignore specific values from being treated as dependencies. Some React hook values, such as useState(), useReducer(), and useRef(), are already ignored by the compiler out of the box.

Here's an example that demonstrates using autorun.ignore() to exclude a value from the dependencies:

import { useCallback, useInsertionEffect, useRef } from 'react';
import { autorun } from 'react-autorun';

export function useCaller<Fn extends (...args: any) => any>(fn: Fn) {
  const ref = useRef(callerRefInit as Fn);

  useInsertionEffect(() => {
    ref.current = fn;
  }, autorun);

  const caller = useCallback((...args: any) => {
    return ref.current(...args);
  }, autorun) as Fn;

  // `useCaller()` return value is now ignored by hooks
  return autorun.ignore(caller);
}

function callerRefInit() {
  throw new Error('Function not ready');
}

With this usage of autorun.ignore, the caller value returned by useCaller() will be excluded as a dependency when used within other hooks. This ensures that the hook won't be invalidated if, for some particular reason, the caller reference has changed.

Installation and Setup

To install React Autorun, use npm:

npm install react-autorun

Next, load the plugin using Babel or SWC.

Babel Setup

If you use Babel, edit your .babelrc file to include react-autorun/plugin/babel:

{
  "plugins": ["react-autorun/plugin/babel"]
}

SWC Setup

If you use SWC with Next.js, edit your next.config.js file to include react-autorun/plugin/swc:

/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    swcPlugins: [
      ['react-autorun/plugin/swc']
    ]
  }
}

module.exports = nextConfig

Please note that loading SWC plugins with Next.js is currently an experimental feature, which may lead to inconsistent results. Make sure to review the Next.js documentation for further details.

License

React Autorun is licensed under the MIT license. See the LICENSE file for more details.