is prefetchQuery the singular form of refetchQueries? when should I use each?
say, on a mutations
onSettled callback. If i know a query I want to refetch then and there, should I use prefetchQuery or refetchQueries?
And also - if my staleTime is 0 (i.e - the default), is there any other benefit to refetching, rather than just querying when it's rendered / active?
say im fine with the stale cache being there for a moment, can I just not worry about it?2 Replies
like-goldOP•3y ago
found this (https://github.com/TanStack/query/discussions/509) discussion distinguishing between the two.
prefetchQuery will create a query if one does not exist or update the data for one if it does. It's basically like using useQuery, but not as a hook refetchQueries is for refetching one (or more) queries that match the query key passed. If none are found, then nothing happens.
like-gold•3y ago
I'd use
invalidateQuery: https://tanstack.com/query/latest/docs/react/guides/invalidations-from-mutationsInvalidations from Mutations | TanStack Query Docs
Invalidating queries is only half the battle. Knowing when to invalidate them is the other half. Usually when a mutation in your app succeeds, it's VERY likely that there are related queries in your application that need to be invalidated and possibly refetched to account for the new changes from your mutation.
For example, assume we have a mu...