T
TanStack•4y ago
genetic-orange

A bit of help with useMutation

New to React Query -- just started today. It's been an absolute breeze. Wonderful work to anyone who has worked on it. I'll send code if necessary, but it's more of a theoretical question. I'm using "useMutation" to send a put request to my server. It updates on the server, but the actual state on my app doesn't change, the data on the site doesn't refresh, unless I click away and click back, or if I actually refresh the page. If this not part of useMutation? Do I need to implement something specific to get this functionality? (similar to adding a dep array in useEffect). I did attempt to use onSuccess to run queryClient.setQueryData to get what I needed to no avail. I also read about optimistic updates. Surely something I'll use eventually but isn't using it without properly refreshing the data on the page technically bad practice because you're then relying on multiple sources of truth? Thanks in advance for the help on this.
2 Replies
genetic-orange
genetic-orangeOP•4y ago
edit: figured it out. thanks anyways! 🙂 How did you solve it out of interest? queryClient.invalidateQueries Looking to solve is using the returned object though, just a bit complex because the returned object is pretty complicated
foreign-sapphire
foreign-sapphire•4y ago
Recently dealt with this - using the object returned out of the mutation can be funky because you have to reconcile it with the result of your queries. We've generally found it easier to useMutation to update the server, then invalidate queries, and have the refreshed queries provide the data that would be provided as the result of the mutation, but as part of their normal behavior. Then if you use optimistic updates it's even better.

Did you find this page helpful?