Optimistic Updates#
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:
- in the
onStartphase of a mutation, you manually set the cached data viaupdateQueryResult - then, in
onError, you roll it back viapatchQueryResult. You don't have to worry about theonSuccesslifecycle here.
Example optimistic update mutation