T
TanStack3w ago
wise-white

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
wise-white
wise-whiteOP3w 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.
genetic-orange
genetic-orange3w ago
Sounds like you'd setQueryData(['some-key'], oldData => ...) for places that need a refreshed profile picture
inland-turquoise
inland-turquoise3w 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.
wise-white
wise-whiteOP3w 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?