-
Hi all I've been setting up optimistic updates for a messaging feature. I have everything working nice except I would like to setup a rollback onError. The console.log at the bottom confirms the failed message has been filtered out of the returned newData. I created some white space at the bottom of the code block at the relevant onError section for readability
At the component level:
is not firing after the rollback function runs I'm confused why the cache is not updating like it does for me during the mutation. Please help, thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
You should probably snapshot your old value in As to why it doesn't work, I think it's because currently you're mutating the page in your map. You're only doing a shallow copy of the "cache", and when you're mapping you're not returning a new object of the page. |
Beta Was this translation helpful? Give feedback.
-
Ah, yes I should have referenced the docs! It's working great now! Thank you for the guidance and explanation! |
Beta Was this translation helpful? Give feedback.
You should probably snapshot your old value in
onMutate
and return it. See the docs.Doing that would make your code a lot easier - rather than having to filter in onError - you would just do something like
queryClient.setQueryData(previousValue)
.As to why it doesn't work, I think it's because currently you're mutating the page in your map. You're only doing a shallow copy of the "cache", and when you're mapping you're not returning a new object of the page.