Feature suggestion: Allow custom list of hooks to ignore on exhaustive-deps
See original GitHub issueThe exhaustive-deps
lint rule doesn’t require setters from useState
, dispatch from useReducer
, and refs from useRef
in dependencies, because we know for sure those will never change. I’d like to be able to add additional custom hooks to that “safe list”. I understand this is potentially very dangerous, but we have a handful of hooks that simply wrap useRef
with some logic. The returned result is essentially just a useRef
result, so it’s perfectly safe to ignore as a dependency.
I’m imagining something like this in the eslint config:
{
"rules": {
// ...
"react-hooks/exhaustive-deps": ["warn", {
"safeHooks": "(useRefWrapper|useOtherRefWrapper)"
}]
}
}
Where useRefWrapper
might look like so:
const useRefWrapper = () => {
const ref = useRef();
if (ref.current) {
console.log('hi!');
}
return ref;
}
Does this seem like a reasonable option, assuming the documentation has a clear caveat mentioning the dangers? If so, I’m happy to work on implementation.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:31
- Comments:5
Top Results From Across the Web
Understanding the React exhaustive-deps linting warning
This ESlint plugin will ensure you are following the rules of Hooks in your React code, which are: Only call Hooks at the...
Read more >Custom hooks with dependency lists and eslint-plugin ...
The react-hooks/exhaustive-deps rule allows you to check your custom ... This option accepts a regex to match the names of custom Hooks that ......
Read more >Rules of Hooks
They let you use state and other React features without writing a class. Hooks ... Call Hooks from custom Hooks (we'll learn about...
Read more >5 Tips to Help You Avoid React Hooks Pitfalls
The React Hooks feature was proposed in October 2018 and released ~4 ... who are annoyed by the exhaustive deps plugin, so let...
Read more >You probably shouldn't ignore react-hooks/exhaustive- ...
It's tempting—we've all been there. We get the react-hooks/exhaustive-deps linting warning about a dependency array in a React hook.
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
While a simple regex would work for some custom hooks, there are many that return arrays and objects that could also benefit from this.
If the config allows any form of data, each hook could specify stable values with an array: numbers for element indices or strings for object properties. Not sure what you’d assign to hooks like
useRefWrapper()
that return a single value. An empty array?Another option would be to pack each hook into an array similar to how other ESLint rules are configured. The hook name would go into the first element followed by the stable array elements or object properties or by itself if its single return value is stable.
Another example: