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

eslint-plugin-react-hooks-signals

v1.0.0

Published

ESLint rules for React Hooks, but supports @preact/signals

Downloads

262

Readme

eslint-plugin-react-hooks-signals

This ESLint plugin is a modified version of React's official hooks plugin. This provides a new rule called "exhaustive-deps-signals" that is intended to be used instead of React's "exhaustive-deps" if you use @preact/signals-react in your project.

You should still include React's eslint hooks plugin since it provides the "rules-of-hooks" eslint rule, but you should disable their version of "exhaustive-deps".

Installation

# npm
npm install -D eslint-plugin-react-hooks-signals
# yarn
yarn add -D eslint-plugin-react-hooks-signals

Example .eslintrc.js

module.exports = {
  …,
  extends: [
    …,
    'plugin:react-hooks-signals/recommended',
  ]
  rules: {
    …,
    'react-hooks/exhaustive-deps': 'off',
  }
}

Explanation

The official React lint rule "react-hooks/exhaustive-deps" doesn't play nicely with signals.

Consider this example:

import { signal } from "@preact/signals-react";
const countSignal = signal(0);

export function Counter({ multiplier: number }) {
  useEffect(() => {
    localStorage.setItem(
      "countWithMultiplier",
      `${multiplier * countSignal.value}`
    );
  }, [multiple, countSignal.value]);

  return (
    <div>
      {multiplier * countSignal.value}
      <button onClick={() => countSignal.value++}>Add One</button>
    </div>
  );
}

The above will fail the "react-hooks/exhaustive-deps" check because countSignal.value in the list of useEffect dependencies is declared an error:

Outer scope values like 'countSignal.value' aren't valid dependencies because mutating them doesn't re-render the component.

But with signals it does re-render the component, so it should be a valid dependency!

This eslint plugin provides a rule that does two things different:

  1. It knows that externally declared signals can re-render a component, so its values should be in the dependency array when used in an effect.
  2. It understands that depending on a signal isn't enough, the signal's value needs to be depended on, since the signal container doesn't change when the signal's value update.

In addition to the above, it provides all the existing checks of React's eslint rule, so you should turn off React's "exhaustive-deps" rule to keep this rule from conflicting.

Advanced Configuration

exhaustive-deps-signals can be configured to validate dependencies of custom Hooks with the additionalHooks option. This option accepts a regex to match the names of custom Hooks that have dependencies.

{
  "rules": {
    // ...
    "react-hooks-signals/exhaustive-deps-signals": ["warn", {
      "additionalHooks": "(useMyCustomHook|useMyOtherCustomHook)"
    }]
  }
}

The React Devs suggest to use this option very sparingly, if at all. Generally saying, they recommend most custom Hooks to not use the dependencies argument, and instead provide a higher-level API that is more focused around a specific use case.

License

MIT

Attribution

Modified by Jon Abrams, based on the existing code by Facebook.