-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
RTK Query - Infinite loop when arg is nested object #1526
Comments
I did another test with Pokemon example. I changed the parameter to nested object and it works, but when I change the route - from https://codesandbox.io/s/boring-voice-9n2xk?file=/src/services/pokemon.ts Why this behavior when I have nested object? Is it a bug? |
These are two different things. That you have to keep nested arguments stable is documented.:
Then, there is a "known issue"/"non-optimal behaviour" that if you mount a component with an error state, it will make another request for that endpoint. This is not optimal, but also I don't know what would be a better way of handling this, since most of the time a new request is intended. The question is how that leads into your infinite loop, since it seems that you are not remounting any components anywhere. 🤔 I will try to look into that later, but if you cal already come up with a theory, I'm all up for that :) |
IMO the loop is roughly like so:
So it's expected that a constantly changing arg + failing query causes an infinite loop. Note: If you use |
You can use the skip parameter. Ex: First create a global variable and set it to false. Then initialize it to the skip parameter. If an error occurs in the rtk query when the component is rendered, it can be captured by if (error) {}. Now make that variable true in the if the condition Ex: >>>>> let error = false const component = () => {
} |
#1533 addressed this and is available in the 1.7 betas. |
saved my day, thanks |
The problem
The problem infinite loop with multiple calls occurs when filter is an object inside another object, which assembles arg.
If filter is a direct value, as string or number, or the object with the same key, the problem does not occur, example:
The "useGetUsersQuery" (example below) hook will make multiple calls as if it were in loop.
I don't know if this is expected behavior with wrong use or a possible bug, however I would like to understand the hook behavior based on prev Arg and next Arg, how does Hook handle this?
Full example with infinite loop
I did some examples and got different results
From the above results, I believe the internal check to see if the args have changed may be causing this issue.
The text was updated successfully, but these errors were encountered: