TanStackT
TanStack16mo ago
10 replies
sad-indigo

Should gcTime refer to the cached entries, or the cache itself

For some reason I was under the impression that gcTime referred to the time an entire query cache attached to a useQuery hook would be considered stale. But it seems like it's applied to each individual cache entry.

So doing

useQuery({ queryKey: ["foo", x], gcTime: 0

will wipe each previously viewed cache entry whenever x changes.

The behavior I'd like to achieve is for a given useQuery hook to keep all cached entries around for some time, until that hook itself unmounts, at which point I'd clear the cache for that query entirely.

The best way I can think of would be for the hook's parent component to just render a QueryClientProvider with its own queryClient. When when it unmounts, and re-mounts, a fresh queryClient (with a fresh cache) should take over.

Would that work properly? (ignoring the fact that I can no longer share cache entries from other queries, elsewhere in the app)

Is there a better way?
Was this page helpful?