React · Middle · Question 46 of 75

How do you implement an optimistic update via the cache in TanStack Query?

Explanation

cancelQueries prevents a background request from overwriting the optimistic data. The snapshot from getQueryData is returned from onMutate as context for rollback. v5 has a simpler option - show the mutation's variables in the UI while it's isPending, without touching the cache.

Further reading

Problem with a question

React · Middle · question 46

Test yourself

Take the React · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz