question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

eslint-plugin-react-hooks useEffect autofix of adding function names causes a lot of infinite loops

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Comments:20 (9 by maintainers)

github_iconTop GitHub Comments

7reactions
gaearoncommented, Mar 12, 2019

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 to useCallback every time.

We could offer an option that disables the autofix when we detect function deps.

1reaction
gaearoncommented, Aug 2, 2019

Regarding the autofix, I hope https://github.com/eslint/rfcs/pull/30 will let us solve the problem without compromising on IDE suggestions.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found