useMutation(): different components are not re-rendered when cache is updated #7828
Unanswered
ashishjmeshram
asked this question in
General
Replies: 1 comment
-
you need to make updates in a fully immutable way. This is not an immutable update:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We are migrating from v3 to v4.
Our codes are starting to break where we are using useMutation and in onSuccess callback of useMutations we are trying to Optimistically update to the new value by using
queryClient.setQueryData
as shown below.queryClient.setQueryData(USER_KEY, { ...previousUser });
This used to work in react query v3 but not working in react query v4.
Surprisingly, this code works if useQuery and mutate function are in same component but not in different component.
So, possible solution is to use structuralSharing:false or update the object in immutable way.
But I am confused and not getting as why it works in same component.
Below is an excerpt of the cache update code from sandbox
Above code seems to work if useQuery and useMutation are called from a single component.
SingleUserUpdateAll.tsx
But it does not work if they are in the different component.
SingleUser.tsx
SingleUserUpdate.tsx
If I use
structuralSharing:false
in useQuery then it works.So, without
structuralSharing:false
it does not re-render SingleUser.tsx component which displays updated user.Now this particular behaviour is specific to react query v4. This works fine in v3 even without
structuralSharing:false
.But, I see that
structuralSharing:true
by default even in react query v3.So, whats breaking in v4?
Beta Was this translation helpful? Give feedback.
All reactions