-
Notifications
You must be signed in to change notification settings - Fork 47k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
eslint-plugin-react-hooks useEffect autofix of adding function names causes a lot of infinite loops #15084
Comments
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). We could offer an option that disables the autofix when we detect function deps. |
That would be a better fix. I think a lot of people end up on your original code example #14326 (comment). |
You could leave autofix if there was a better solution for detecting infinite async loops. |
Thanks, I updated that example. |
@gaearon I'm sorry for the completely unrelated question in the topic (feel free to delete it) |
We've been using it at FB for a few weeks now and are pretty happy with it. |
I have a problem when
Adding the |
I have this shortterm fix:
I very well could be approaching this wrong :) |
Maybe I'm missing something, but how would I prevent infinite loops in this example? By adding EDIT: Solved it with |
Hello, the eslint rule is creating an infinite loop in my case too: I'm guessing there is a better way to do what i do without the infinite loop and disabling eslint. TLDR: If i have a token or the token change, i make a call to my backend to get a list of artists. In both cases, i use in the PS: the solution for @goodoldneon is not working in my case, when i copy his code into my editor, the autoformat is adding the |
@wcastand, is your editor saying you need the |
Yeah, i'm using vscode if that helps. The auto format/fixing is adding the setters and vars in the array and that creates an infinite loop. |
@wcastand The correct fix in your case is to wrap Please read the documentation which explains all of these cases one by one: |
Thanks for the help, sorry for the delayed answer, i'll try this out when i can but the project was based on spotify api and i am hitting limit rate for various reason and limitation of their API. Anyway thanks for the example, this issues won't be issues anymore after Suspense and |
@gaearon I made a reproduction of a custom hook to work with array: https://codesandbox.io/s/hopeful-meadow-629zh The set function is re-exported from the dipatcher of useState(), however eslint throw error as I'm calling it from the object. Wonder what is your thought as to whether this is a valid case? My thinking is that I could eitehr useReducer, or export my hook as an array, or memo the object I'm exporting from my hook :-? ... |
Yeah you can memo that object. |
It would probably make sense to me if you didn’t keep using the object directly. But destructure individual functions from it instead. Then they can be more stable (for example a setter would never need to change). You could useCallback for those individual functions. |
Regarding the autofix, I hope eslint/rfcs#30 will let us solve the problem without compromising on IDE suggestions. |
Let's consolidate and track this in #16313 instead. |
This is resolved now. |
I've read "My function value is constant" from #14920 (comment).
If you autofix the
useEffect
, would it also be possible to autofix any functions added by wrapping them in auseCallback
at the same time?This would greatly improve the user experience.
The text was updated successfully, but these errors were encountered: