When 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,
onQueryStartedwill be executed
- you manually update the cached data by dispatching
- then, in the case that
promiseResultrejects, you roll it back via the
.undoproperty of the object you got back from the earlier dispatch.
or, if you prefer the slightly shorter version with