Optimistic Updates
Last updated
Was this helpful?
Last updated
Was this helpful?
When you're performing an update on some data that already exists in the cache via , 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.
or, if you prefer the slightly shorter version with .catch