Difference between `fetchQuery` and `ensureQueryData`?
The docs for
queryClient.ensureQueryData says:
ensureQueryData is an asynchronous function that can be used to get an existing query's cached data. If the query does not exist, queryClient.fetchQuery will be called and its results returned.But, isn't that what
fetchQuery already does kind of? Returns the query data if it's already fetched, and does the fetch if it's not?2 Replies
flat-fuchsia•3y ago
No, fetchQuery always fetches unless you specify staleTime, at which point it might return fresh data. But ensureQueryData always immediately returns data if there is any, no matter if it's stale or not. The implementation is:
queryClient.getQueryData(key) ?? queryClient.fetchQuery(...)deep-jadeOP•3y ago
Ooooh, OK, that makes sense, thank you 👍