eslint-plugin-react-hooks useEffect autofix of adding function names causes a lot of infinite loops
See original GitHub issueI’ve read “My function value is constant” from https://github.com/facebook/react/issues/14920#issuecomment-471070149.
There is a problem on the opposite spectrum of this, which is where you get infinite loops (a function value always changes). We catch that in the lint rule now when possible (in the same component) and suggest a fix. But it’s tricky if you pass something several levels down.
If you autofix the useEffect
, would it also be possible to autofix any functions added by wrapping them in a useCallback
at the same time?
This would greatly improve the user experience.
Issue Analytics
- State:
- Created 5 years ago
- Comments:20 (9 by maintainers)
Top Results From Across the Web
How to solve the React useEffect Hook's infinite loop patterns
Let's get started! What causes infinite loops and how to solve them. Passing no dependencies in a dependency array. If your useEffect function...
Read more >How to Solve the Infinite Loop of React.useEffect()
1. The infinite loop and side-effect updating state. A functional component contains an input element. Your job is to count and display how...
Read more >Obeying react-hooks/exhaustive-deps leads to infinite loops ...
Providing the getUsers functions to a useEffect call makes the react-hooks/exhaustive-deps eslint rule unhappy, because it wants the getUsers ...
Read more >5 Tips to Help You Avoid React Hooks Pitfalls - Kent C. Dodds
Let's explore some hook gotchas and how to avoid them.
Read more >5 utility React Hooks for every project - Zero Molecule
Simple React Hooks for everyday projects to save you some time and make your ... function to run effect because we can potentially...
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
A much better fix is usually to either move a function either outside of the component (if it doesn’t use props or state), or move it inside the effect (if it does).
useCallback
is the escape hatch for when you can’t or don’t want to do either. So I don’t think we’d want to autofix it touseCallback
every time.We could offer an option that disables the autofix when we detect function deps.
Regarding the autofix, I hope https://github.com/eslint/rfcs/pull/30 will let us solve the problem without compromising on IDE suggestions.