Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] addReaction.onQueryStarted should also update getPost(id) query cache. #47

Open
nandin-borjigin opened this issue Dec 30, 2022 · 2 comments

Comments

@nandin-borjigin
Copy link

nandin-borjigin commented Dec 30, 2022

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).

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()
}
},

@Rockung
Copy link

Rockung commented Mar 31, 2023

How to update the cache for getPost(id)?

@markerikson
Copy link
Collaborator

Very belated answer, but yeah, good catch! I missed that.

I'm working on some updates to the example app and tutorial, and will include that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants