T
TanStack3y ago
adverse-sapphire

Same update for multiple queries

My app has a general feed with posts of all the subcommnuties under the queryKey ['posts', communityId]. But you can go directly to one community and scroll over the posts of that community (queryKey: ['posts', communityId, subCommunityId]) And if you enter to see post details you have the queryKey ['post', 'communityId, postId] My problem is that every mutation hook that dispatch an action like 'comment', 'upvote', etc ends in three setQueryData that the search in the list of posts to update the correct post. onSuccess: setQueryData<Post[]>( ['posts', communityId], () => new posts array...) setQueryData<Post[]>(['posts', communityId, subCommunityId], new posts array...) setQueryData<Post>( ['post', 'communityId, postId], () => new post ...) The action is the same (for exmple: increment likes counter). It its possible to make only one setQueryData with the same action for different queryKeys?
1 Reply
other-emerald
other-emerald2y ago
https://tanstack.com/query/v5/docs/react/reference/QueryClient#queryclientsetqueriesdata Try this and only pass a predicate function to the filter that chooses which one to set data for.
QueryClient | TanStack Query Docs
QueryClient The QueryClient can be used to interact with a cache:

Did you find this page helpful?