Steps for mutating multiple infinite lists #7616
Answered
by
TkDodo
codinginflow
asked this question in
Q&A
-
Let's say I want to delete a resource (a post) and remove it from different feeds that use Are these the correct steps to do that? Merely invalidating the feeds is not enough because the more pages are in cache, the longer it takes, causing quite a long delay. I cancel the queries first because otherwise, it seems to wait for revalidation to finish before we mutate the cache. const mutation = useMutation({
mutationFn: deletePost,
onSuccess: async () => {
await queryClient.cancelQueries({ queryKey: ["post-feed"] });
queryClient.setQueriesData(
{ queryKey: ["post-feed"] },
(oldData: InfiniteData<PostsPage, unknown> | undefined) => {
if (!oldData) return;
return {
...oldData,
pages: oldData.pages.map((page) => {
page.posts = page.posts.filter((p) => p.id !== post.id);
return page;
}),
};
},
);
queryClient.invalidateQueries({ queryKey: ["post-feed"] });
[...]
},
onError(error) {
[...]
},
}); |
Beta Was this translation helpful? Give feedback.
Answered by
TkDodo
Jun 25, 2024
Replies: 1 comment 1 reply
-
looks good |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
codinginflow
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
looks good