-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
too many re-renders when changing keys #3772
Comments
Note: this used to work in see: |
Hello, is it still available? I would like to try to work on it. Thanks! |
@karol-janik sure, go ahead. Thank you 🙏 |
This appears to be the order of operations when the query key changes:
states.length counts after each operation:
The issue is there is a state update that changes key which rerenders **edit
Seems solution might be to disable the redundant notify on key changes? |
I noticed that this |
@karol-janik I'm not sure we can prevent/memo the change. Might be easier to not fire the "fetch" event from react-query observer when the key changes since this is an extra event that causes the extra "loading" state to appear. |
@karol-janik this setState cannot cause any re-renders because the setter of that useState is not used: query/packages/react-query/src/useBaseQuery.ts Lines 71 to 77 in 69ba565
this is the correct way to initialize a value once, when the component mounts. |
is this still an issue ? i'm on
|
It seems to be the same issue in the practice of React Query lesson: There is 3 re-renders instead of 2 |
remove |
@suheth this issue is open for a reason, namely, because it hasn't been fixed. the issue I linked that re-introduced this extra re-render mentions that:
so I'm not sure if we can just easily optimize this |
@abacaj do all tests pass with your change? Please read the comment above ⬆️ . I think it is not guaranteed that calling |
seems like this indeed an upstream issue that rtk-query has as well: |
Is there any workaround to avoid the unnecessary render? |
Sorry @SaadUrRehmanBaig but It just happens on the next query. In the author's example, The issue will happens when we click the next button. Please check this again. On your case, we have:
I confirmed I faced this issue on |
it should be fixed when the bugs in react are fixed 🤷 |
I think the same issue is addressed in #5538; and this PR may solve it. Here is the same sandbox with the built version from the PR that shows the resolve actually: https://codesandbox.io/s/react-query-re-renders-forked-66s9td?file=/src/App.tsx |
amazing @incepter 🙌 |
looks like thePR has been reverted? Using refetchInterval seems to cause full page re renders |
@daniel-ricado the PR has not been reverted. If you see behaviour you don't expect, please file a separate issue with a new reproduction |
hmmm bumping up the version didn't resolve it, but I'm thinking it's refreshing all children of the component it's used in. yup will setup a reproduction example and new issue if thats the case cheers @TkDodo . |
It worked for me with request aborting |
Describe the bug
When changing the query key, react-query re-renders the component two times with the exact same states
Your minimal, reproducible example
v4: https://codesandbox.io/s/react-query-re-renders-t2z4u8, v3: https://codesandbox.io/s/react-query-re-renders-v3-cfo8y4
Steps to reproduce
next
buttonThe second "loading" render has the exact same state as the previous one and is unnecessary.
This happens on v17 and v18 of react, and in v3 and v4 of react-query.
It also has nothing to do with extra renders in dev-mode and strict effects (the example doesn't use strict mode), and it can also be reproduced when the logging is moved to an effect. The component really renders twice.
Expected behavior
we should see two renders instead of three:
How often does this bug happen?
Every time
Screenshots or Videos
Platform
independent (I'm on macos & brave)
react-query version
v3 and v4
TypeScript version
No response
Additional context
first reported on stackoverflow: https://stackoverflow.com/questions/72834988/react-query-makes-component-to-rerender-multiple-times
my best guess is that
setState
triggers one render, and thenuseQuery
re-renders again. But those should be deduped....here's a failing test-case to reproduce:
The text was updated successfully, but these errors were encountered: