T
TanStack3mo ago
ratty-blush

tanstack query as a cache

Hi, I am using tanstack query instead of default router cache. Example from docs is using:
// Since we're using React Query, we don't want loader calls to ever be stale
// This will ensure that the loader is always called when the route is preloaded or visited
defaultPreloadStaleTime: 0,
// Since we're using React Query, we don't want loader calls to ever be stale
// This will ensure that the loader is always called when the route is preloaded or visited
defaultPreloadStaleTime: 0,
What is the difference between defaultStaleTime vs defaultPreloadStaleTime? That's my loader: which option is responsible for making it run only once -> The one in the querOptions or any of those 2 above?
return queryClient.ensureQueryData({
queryKey: ["lobby"],
queryFn: getLobby,
staleTime: Number.POSITIVE_INFINITY,
});
return queryClient.ensureQueryData({
queryKey: ["lobby"],
queryFn: getLobby,
staleTime: Number.POSITIVE_INFINITY,
});
9 Replies
ratty-blush
ratty-blushOP3mo ago
Should I also set defaultStaleTime to 0?
correct-apricot
correct-apricot3mo ago
"run once" per what?
ratty-blush
ratty-blushOP3mo ago
just once, same as with basic tanstack query stale time Infinity disable refetches and it runs once...
correct-apricot
correct-apricot3mo ago
ah yes then the in the queryclient is responsible for that the others disable caching in router as this is handlded by query then
ratty-blush
ratty-blushOP3mo ago
No description
ratty-blush
ratty-blushOP3mo ago
This kitchensink doesn't disable the defaultStaleTime only preload why? https://tanstack.com/router/latest/docs/framework/react/examples/kitchen-sink-react-query-file-based So my understanding is: Settings both to 0 will make loader run on every attempt to prefetch / load but since we use tanstack query then this data would come from cache
correct-apricot
correct-apricot3mo ago
preloadStaleTime defaults to defaultPreloadStaleTime ah wait defaultStaleTime is 0 by default
ratty-blush
ratty-blushOP3mo ago
okey thanks I guess all clear now @Manuel Schiller so when I am using queryClient as cache then I shouldn't use hook useLoaderData and just use query client for getting the data that's fetched in loader?
correct-apricot
correct-apricot3mo ago
yes

Did you find this page helpful?