State Management · Senior · Question 21 of 75

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

Explanation

cancelQueries prevents an in-flight refetch from overwriting the optimistic data; the snapshot from getQueryData is returned as context for rollback. v5 also has a simpler approach: render variables while the mutation isPending, without touching the cache.

Further reading

Problem with a question

State Management · Senior · question 21

Test yourself

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

Take this quiz