State Management · Senior · Question 30 of 75
How do you do an optimistic update in RTK Query?
- ✓Correct answer:
updateQueryDatainonQueryStarted, thenpatchResult.undo()ifqueryFulfilledrejects - 2Pass
optimistic: trueto the endpoint definition - 3Invalidate tags before sending the request
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.