Refetch a prefetch with interval in background #1920
-
Is there a way to refetch a prefetch with interval? I prefetch data from the root component to load critical data upfront: export function useBatchPrefetch(batchId: string): void {
const queryClient = useQueryClient();
queryClient.prefetchQuery(["batch", batchId], () => fetchBatch(batchId));
}; in App.tsx: function App(): JSX.Element {
...
useBatchPrefetch("someBatchId")
...
return <div>....</div>
} I use another hook that fetches and provides the data "normally" (with export function useBatch(
batchId: string
): {
isLoading: boolean;
batch: Batch | null;
} {
const { isLoading, data } = useQuery(
["batch", batchId],
() => fetchBatch(batchId),
{
refetchInterval: 1 * 60 * 1000,
refetchOnWindowFocus: false,
cacheTime: Infinity,
}
);
return { isLoading, batch: data?.result || null };
}; Components that need that data to show something are using the I'm using the Is this done via |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
As far as i know, prefetchQuery does nothing if there is already data in the cache. I’m not exactly sure what you are trying to achieve. Once you have data in the cache, if a new component mounts, it will use that cached data and do a background refetch to get the latest data. if you want to fetch in an interval without having an actual useQuery, i think you want to use |
Beta Was this translation helpful? Give feedback.
As far as i know, prefetchQuery does nothing if there is already data in the cache. I’m not exactly sure what you are trying to achieve. Once you have data in the cache, if a new component mounts, it will use that cached data and do a background refetch to get the latest data.
if you want to fetch in an interval without having an actual useQuery, i think you want to use
queryClient.fetchQuery
in auseInterval
. Dan Abramov shows a good impl of that hook here: https://overreacted.io/making-setinterval-declarative-with-react-hooks/