#
Optimistic UpdatesWhen you're performing an update on some data that already exists in the cache via useMutation
, RTK Query gives you a few tools to implement an optimistic update. This can be a useful pattern for when you want to give the user the impression that their changes are immediate.
The core concepts are:
- when you start a query or mutation,
onQueryStarted
will be executed - you manually update the cached data by dispatching
api.util.updateQueryData
- then, in the case that
promiseResult
rejects, you roll it back via the.undo
property of the object you got back from the earlier dispatch.
- TypeScript
- JavaScript
Optimistic update mutation example (async await)
Optimistic update mutation example (async await)
or, if you prefer the slightly shorter version with .catch