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-addons

v0.4.1

Published

ESLint rule to check unused and potentially unnecessary dependencies in React Hooks.

Downloads

56,147

Readme

eslint-plugin-react-hooks-addons

ESLint rule to check unused and potentially unnecessary dependencies in React Hooks.

NPM npm downloads

Why?

eslint-plugin-react-hooks is awesome for linting the dependency array of React Hooks. But it doesn't do one thing: unused dependencies in the useEffect or useLayoutEffect hooks are not reported. Unused variables in useEffect's dependency array are perfectly valid in some use cases. However, they might be unnecessary in some other cases which cause the effect hook to run unintentionally.

Take the following code as an example:

const [user1, setUser1] = useState();
const [user2, setUser2] = useState();

useEffect(() => {
  fetch(`someUrl/${user1}`).then(/* ... */);
  fetch(`someUrl/${user2}`).then(/* ... */);
}, [user1, user2]);

Next day you update the code and remove the second fetch but forget to remove user2 from the dependency array:

const [user1, setUser1] = useState();
const [user2, setUser2] = useState();

useEffect(() => {
  fetch(`someUrl/${user1}`).then(/* ... */);
}, [user1, user2]);

Then the useEffect will run whenever user1 or user2 changes, which is probably not your intention. Similar errors occur more frequently when the hook callback function is large and there is a long dependency array. This eslint plugin checks and reports this kind of error.

What if I have a value which is not used in the hook function scope but I want the effect hook to run whenever that value has changed? You could prepend a /* effect dep */ comment to the value in dependency array then it will be skipped during linting. It brings an addition benefit: the value is explicitly marked as effectful so that other people coming across the code will understand it's not a programmatic error.

useEffect(() => {
  fetch(`someUrl/${user1}`).then(/* ... */);
- }, [user1, user2]);
+ }, [user1, /* effect dep */ user2]);

Install

with npm

npm install -D eslint-plugin-react-hooks-addons

or with Yarn

yarn add -D eslint-plugin-react-hooks-addons

Usage

Flat config

import reactHooksAddons from 'eslint-plugin-react-hooks-addons';

export default [
  reactHooksAddons.configs.recommended
  // other configs...
];

Or, use a custom configuration:

import reactHooksAddons from 'eslint-plugin-react-hooks-addons';

export default [
  // other configs...
  {
    plugins: {
      'react-hooks-addons': reactHooksAddons
    },
    rules: {
      'react-hooks-addons/no-unused-deps': 'warn'
    }
  }
];

Legacy config

{
  "extends": ["plugin:react-hooks-addons/recommended-legacy"]
}

Or, use a custom configuration:

{
  "plugins": ["react-hooks-addons"],
  "rules": {
    "react-hooks-addons/no-unused-deps": "warn"
  }
}

Effect deps

Explicitly mark a dependency as effectful with /* effect dep */ comment:

useEffect(() => {
  // ...
}, [unusedVar, /* effect dep */ effectVar]);

Then only the unusedVar will be reported as an unused dependency.

Options

effectComment

You can use a different comment to mark dependencies as effectful:

"rules": {
  "react-hooks-addons/no-unused-deps": [
    "warn",
    {
      "effectComment": "effectful"
    }
  ]
}

additionalHooks

The rule checks useEffect and useLayoutEffect hooks by default. It can be configured to check dependencies of custom hooks with the additionalHooks option. This option accepts a pattern key which is a regex pattern. If you set the replace key to true, it would replace the default hooks.

"rules": {
  "react-hooks-addons/no-unused-deps": [
    "warn",
    {
      "additionalHooks": {
        "pattern": "useMyCustomHook|useMyOtherCustomHook",
        "replace": true
      }
    }
  ]
}

Note: this eslint plugin is supposed to work in tandem with eslint-plugin-react-hooks, as it doesn't check things that have already been reported by that plugin.

License

MIT Licensed.