-
Notifications
You must be signed in to change notification settings - Fork 856
Open
Description
In the RTK version of the tutorial, addReaction mutation optimistically updates the reaction data in the getPosts cache. But it should also update the cache for getPost(id) query as well. Otherwise, addReaction mutation is not reflected in the SinglePostPage.
I cannot contribute a PR because this version of code doesn't have a corresponding branch (it only has a tag).
redux-essentials-example-app/src/features/api/apiSlice.js
Lines 43 to 60 in dcc6064
| async onQueryStarted({ postId, reaction }, { dispatch, queryFulfilled }) { | |
| // `updateQueryData` requires the endpoint name and cache key arguments, | |
| // so it knows which piece of cache state to update | |
| const patchResult = dispatch( | |
| apiSlice.util.updateQueryData('getPosts', undefined, (draft) => { | |
| // The `draft` is Immer-wrapped and can be "mutated" like in createSlice | |
| const post = draft.find((post) => post.id === postId) | |
| if (post) { | |
| post.reactions[reaction]++ | |
| } | |
| }) | |
| ) | |
| try { | |
| await queryFulfilled | |
| } catch { | |
| patchResult.undo() | |
| } | |
| }, |
Metadata
Metadata
Assignees
Labels
No labels