T
TanStack3mo ago
quickest-silver

Cache update

Is it normal to call several custom functions to update the cache after a mutation when using TanStack Query, or is that considered ineffective?
4 Replies
quickest-silver
quickest-silverOP3mo ago
For example, if the current user updates their profile picture, and some posts are tied to that user's data but the user isn't directly part of the query keys then those posts won't automatically reflect the updated profile picture. In such cases, it's often necessary to manually update or invalidate multiple related queries using custom functions.
ugly-tan
ugly-tan3mo ago
Sounds like you'd setQueryData(['some-key'], oldData => ...) for places that need a refreshed profile picture
rival-black
rival-black3mo ago
That's why invalidation is preferred, as you can target all relevant queries with one line of code and they will be eventually consistent.
quickest-silver
quickest-silverOP3mo ago
is this invalidating this way redundant?
const queryClient = getQueryClient();

queryClient.invalidateQueries({
queryKey: threadOptions.queryKey,
});

queryClient.invalidateQueries({
queryKey: threadInfiniteOptions.queryKey,
});
const queryClient = getQueryClient();

queryClient.invalidateQueries({
queryKey: threadOptions.queryKey,
});

queryClient.invalidateQueries({
queryKey: threadInfiniteOptions.queryKey,
});

Did you find this page helpful?