You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
v4 regression: setQueryData triggers render after setState #4836
I've run into a problem with setQueryData which has introduced a few logic bugs in an app I'm working on. The issue is that when calling both setState and setQueryData in a nested component that receives its data via props from a query in a parent component, the call to setState triggers a re-render with stale query data even after setQueryData is called. Only after that render completes is a render in the parent component triggered with the data provided to setQueryData. This behaviour differs if the useQuery hook is in the child component, in which case there is only one render which has the correct data.
This behaviour was unexpected and differs from swr. swr triggers a render in the parent component first and has consistent behaviour whether the query hook is in the parent or the child component.
This also appears to be a regression in v4 (assuming it isn't documented somewhere). Using react-query v3.39.2 the parent component renders first.
I think this is another report of the batching problem with useSyncExternalStore, which we use under the hood in v4 (but not in v3). Have a look at this open issue in React:
That does look like it could be the cause. Thanks for finding that!
It does put me in a bit of a predicament though. We're still using React 16, so the upstream fixes are unlikely to land for us for a long time. Could this be worked around in react-query? If not we might have to downgrade to v3 :(
Activity
[-]v4 regression: [/-][+]v4 regression: setQueryData triggers render after setState[/+]TkDodo commentedon Jan 18, 2023
I think this is another report of the batching problem with
useSyncExternalStore, which we use under the hood in v4 (but not in v3). Have a look at this open issue in React:useSyncExternalStorebatching & flushing behaviour intended? react/react#25191ashaarmstrong commentedon Jan 18, 2023
That does look like it could be the cause. Thanks for finding that!
It does put me in a bit of a predicament though. We're still using React 16, so the upstream fixes are unlikely to land for us for a long time. Could this be worked around in react-query? If not we might have to downgrade to v3 :(
TkDodo commentedon Jan 20, 2023
Can you maybe explain why it is an issue in your app?
TkDodo commentedon Feb 20, 2023
I'll close this as it's stale and there probably isn't much we can do on our end.