State Management · Senior · Question 30 of 75

How do you do an optimistic update in RTK Query?

Explanation

dispatch(api.util.updateQueryData('getPost', id, (draft) => { draft.title = t })) patches the cache via Immer and returns a patch object; try { await queryFulfilled } catch { patch.undo() } rolls it back.

Further reading

Problem with a question

State Management · Senior · question 30

Test yourself

Take the State Management · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz