TanStackT
TanStack3y ago
1 reply
sad-indigo

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?
Was this page helpful?