[eslint-plugin-react-hooks] Add option to require functions from core hooks in dependencies
See original GitHub issueDo you want to request a feature or report a bug?
feature
What is the current behavior?
The react-hooks/exhaustive-deps
will currently accept either (and fix neither) of the following:
// Scenario A: Function returned by core hook IS NOT specified as a dependency.
const [value, setValue] = useState(initialValue);
const toggle = useCallback(() => setValue(v => !v), []);
// Scenario B: Function returned by core hook IS specified as a dependency.
const [value, setValue] = useState(initialValue);
const toggle = useCallback(() => setValue(v => !v), [setValue]);
What is the expected behavior?
If we add the following to our .eslintrc.js
// Introduces a `requireCoreFunctions` config option.
'react-hooks/exhaustive-deps': ['error', { requireCoreFunctions: true }]
then the rule should fail in scenario A, and fixing should result in scenario B.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
n/a
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (1 by maintainers)
Top Results From Across the Web
eslint-plugin-react-hooks - npm
This option accepts a regex to match the names of custom Hooks that have dependencies. We suggest to use this option very sparingly,...
Read more >Hooks FAQ - React
Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class. This page...
Read more >eslint-plugin-react-hooks | Yarn - Package Manager
Fast, reliable, and secure dependency management. ... ESLint rules for React Hooks ... Declarative: React makes it painless to create interactive UIs.
Read more >12 essential ESLint rules for React - LogRocket Blog
This rule enforces certain rules about the contents of the dependency array that is passed to Hooks, such as useEffect , useCallback ,...
Read more >szhsin/eslint-plugin-react-hooks-addons - GitHub
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...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
TLDR; it would eliminate a gray area for those who want to be more strict and consistent with their code standards.
With regards to including setState/dispatch as hook inputs/dependencies, you’ve been saying something to the effect of “you don’t have to, but it doesn’t hurt.” (Correct me if I’m wrong!) That makes sense, but leaves the developer with a decision to make and an opportunity to bikeshed.
We would prefer to put this in our code standards, and have the linter enforce it:
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!