Repository navigation
Video: optimistic updates with useMutation, including rollback and the refetch overwrite #11923
Unanswered
thecoderlog
asked this question in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I made a short video that walks through optimistic updates with TanStack Query, aimed at people who already use
useMutationand want the UI to update before the server answers.What it covers:
onMutate: cancel in-flight queries withcancelQueries, snapshot the cache, then write the optimistic value withsetQueryDataonError: roll back to the snapshotonSettled: invalidate so the cache ends up matching the servercancelQueriesmakes the optimistic item flash or get overwritten by a refetch that was already in flightThe core of it:
Video: https://youtu.be/PItSK_8h9gA
Feedback welcome, especially if there's a case you'd want covered next.
All reactions