-
Notifications
You must be signed in to change notification settings - Fork 109
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
Calling a mutation within React.useEffect causes infinite loop #205
Comments
I seems it enters an infinite loop because of "myMutation" in the useEffect second argument. It happens because every time that a mutation calls, returned function "myMutation" also changes. Did you try it without that argument ? I know it causes a warning, but maybe it'll fix your problem. |
Yeah whilst that does fix the problem, unfortunately that goes against the official spec for useEffect and breaks hook linting. I'd like to find a way to fix this without having to disable the linter for that line. |
Did you found a way to solve this issue? Is there a reason for |
Yes, your suggestions do fix the problem but give the lint warning. But the proper way of doing this is to destructure the mutate from the mutation and provide that to the dependency array of use effect! useEffect(() => { |
Omg thank you @yaralahruthik! This solved my identical issue perfectly without requiring weird workarounds :) |
I am also getting an infinite loop and i am using useMutation from '@tanstack/react-query'; const { useEffect(() => { |
In your case the issue does not seem to be in this part of the code. Is it possible that the mutation updates some data which results in re-rendering the component that calls it? Maybe try commenting out parts of code to figure the actual cause? |
Weird API. You end up having to rename mutate and mutateAsync every time in order to get this right. |
Agree this is pretty weird and breaks the normal interior mutability pattern (e.g. seen in |
Agree, weird behaviour, stumbled on it today... |
I'm attempting to perform a mutation when a specific route is hit in my application. When using useEffect and passing through a mutation method as a dependancy, it enters an infinite loop.
This may be by design and my code an anti-pattern, if so I'd love to know an alternative way to trigger a mutation when a component is mounted for the first time.
A similar issue was fixed in
graphql-hooks
recently, if that helps at all.The text was updated successfully, but these errors were encountered: