-
I want to set query cache after mutationFn manually, but it doesn't cause component to rerender. Here are a demo with detailed comments. https://codesandbox.io/s/react-query-setquerydata-xmncm2?file=/src/hook.ts It looks like the React Query will deep compare the cached data before and after update function in setQueryData(), which refer the same object (unlike the setState() behaves in usestate() hooks). So when I tried to change old data in setQueryData(), it shows no changes for React Query, therefore useQuery() won't rerender the component (e.g. comp A in demo), right? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
This is because of Your example would work if you did structuralSharing: (oldData, newData) =>
oldData === newData ? oldData : newData, But that's basically the same thing as |
Beta Was this translation helpful? Give feedback.
-
@TkDodo how will this work when I am using setQueryData instead of useQuery? Basically, One my hooks is use setQueryData to set some data and a component is using getQueryData to use the data. when the component is already mounted then the getQueryData doesn't update when i switch page and revisit the page then the updated data is loaded from getQueryData. |
Beta Was this translation helpful? Give feedback.
This is because of
structuralSharing
and because you are mutating the old value, it's reusing the same data for performance reasons. You shouldn't mutate your old data.Your example would work if you did
structuralSharing: false
for youruseQuery
inuseGetNums
. Or evenBut that's basically the same thing as
structuralSharing: false
.More info about structural sharing