useQuery behavior in v3 is fundamentally different from that in v2 #2518
Replies: 4 comments
-
I'm not sure how https://codesandbox.io/s/zen-goldstine-n4sh4?file=/src/index.js best opened in a separate window: https://n4sh4.csb.app/ according to the docs, rerender can be used to re-render a component with different props, so I'm not sure why you are calling
this is what I am doing here: https://github.com/TkDodo/testing-react-query/blob/fd02fdfc4622ddcef5c3713ef9fc24d9df09298e/src/tests/hooks.test.tsx#L10-L16 I also have a blog post on testing: https://tkdodo.eu/blog/testing-react-query |
Beta Was this translation helpful? Give feedback.
-
let me move that to a discussion and we can re-open an issue once we've undoubtedly identified one. |
Beta Was this translation helpful? Give feedback.
-
@TkDodo Thanks for your reply. Following are the links to codesandbox for v2 and v3. "rendering Example" is logged twice on mount of the component - one for loading state and the other data fetch complete state.
This is the exact behaviour I was trying to demonstrate with tests previously. Please check it out. Thanks. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reproduction. I think the reasoning behind this change has been stated here: #2001 (comment) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the bug
I'm currently working on migrating from
react-query
v2 to v3. I noticed a fundamental difference in the behavior ofreact-query
and it makes a huge difference. (especially in our test suite)useQuery
inreact-query
v3 undergoes one extra re-render when compared to v2. I have test cases below to prove it.In the screenshot below, I'm simply testing out the useQuery hook and results after subsequent renders. (use both v2 and v3)
In case of v3, I must wait for two render cycles to get the results. (check the highlighted box) While in case of v2, I get the results after one render cycle.
Our tests at many places are banking on this behavior and because of an extra asynchronous cycles, the tests are breaking with
act
errors.Am I doing something wrong? Is this behaviour intentional? Can something be done to modify this behavior?
As a user, I'd not prefer unnecessary re-renders. I feel that optimizing this behavior would the right thing to do.
To Reproduce
The following two repositories have tests included for v2 and v3 testing out
useQuery
.https://github.com/suhasv1995/rq-v2-test
https://github.com/suhasv1995/rq-v3-test
Expected behavior
useQuery
behavior in v3 should be similar to that in v2.Screenshots

Beta Was this translation helpful? Give feedback.
All reactions