Skip to content

Eslint plugin for react hooks exhaustive-deps check with static value tracking

License

Notifications You must be signed in to change notification settings

wogns3623/eslint-plugin-better-exhaustive-deps

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Better react exhaustive-deps eslint plugin with static variable check

Installation

yarn add -D @wogns3623/eslint-plugin-better-exhaustive-deps

or

pnpm i -D @wogns3623/eslint-plugin-better-exhaustive-deps

or

npm i -D @wogns3623/eslint-plugin-better-exhaustive-deps

Usage

To use this plugin, you must disable original react-hooks/exhaustive-deps rule

module.exports = {
  // ...
  plugins: ["@wogns3623/better-exhaustive-deps"],
  rules: {
    "react-hooks/exhaustive-deps": "off",
  }
  // ...
}

checkMemoizedVariableIsStatic

Check variable memoized by useCallback or useMemo is static.

If Deps is empty or filled with static variable, the return value is also treated as a static variable.

module.exports = {
  // ...
  plugins: ["@wogns3623/better-exhaustive-deps"],
  rules: {
    // disable original rule
    "react-hooks/exhaustive-deps": "off",
    "@wogns3623/better-exhaustive-deps/exhaustive-deps": [
      "warn",
      {
        checkMemoizedVariableIsStatic: true,
      },
    ],
  },
  // ...
};

staticHooks

Return values ​​registered as true are treated as static variable.

Can configure destructured value independently.

module.exports = {
// ...
  plugins: ["@wogns3623/better-exhaustive-deps"],
  rules: {
    // disable original rule
    "react-hooks/exhaustive-deps": "off",
    "@wogns3623/better-exhaustive-deps/exhaustive-deps": [
      "warn",
      {
        staticHooks: {
          useCustomRef: true,
          useSomething: [false, true],
          useSomethingOther: {
            value: false,
            callback: true
          },
        },
      },
    ],
  },
// ...
};

use grncdr's code facebook/react#16873 (comment)

TODO

  • Check callback generated by useCallback is immutable
  • Create a rule to check for unnecessary static (stable) values in dependency list.
  • Add presets for popular React libraries
  • Add automatic inference for whether a custom hook return value is static or not

About

Eslint plugin for react hooks exhaustive-deps check with static value tracking

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published