1. 20
    Undo an Optimistic Update in RTK Query with queryFulfilled
    1m 3s

Undo an Optimistic Update in RTK Query with queryFulfilled

Jamund Ferguson
InstructorJamund Ferguson
Share this video with your friends

Social Share Links

Send Tweet
Published a year ago
Updated a year ago

The thing about optimistic updates is that sometimes the worst case happens: your server fails. RTK Query makes handling this case incredibly easy.

In your onQueryStarted handler you have access to the original promise (similar to the results of unwrap()) in a value called queryFulfilled. With this value you can add a .catch() which will be ready to handle any non-200 response and in there you can call .undo() on the results of dispatching api.util.updateQueryData.

Doing that will revert your redux and rtk query state to where it was before your made the change. It's honestly kind of magical.

The document for optimistic updates and undo can be found here: https://redux-toolkit.js.org/rtk-query/usage/manual-cache-updates#optimistic-updates

egghead
egghead
~ 31 seconds ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today