Prefetching infinite queries #826
-
I wanted to prefetch the first page of an infinite query, but the docs don't seem to cover this. I was able to find a solution by reading the code, but it uses an undocumented option. I'm wondering if this option and its use for prefetching infinite queries should be documented, or if a different API might be more desirable. Problem
SolutionLooking through the code, I found that ExampleI ended up factoring out a helper function to ensure my query arguments stay in sync between the fetch and prefetch call sites. function getQueryArgs(filters) {
return [
['bikes', filters], // queryKey
fetchBikes, // queryFn
{
infinite: true, // implied by useInfiniteQuery, but needed for prefetching to work (undocumented)
getFetchMore: lastGroup => lastGroup.results.endCursor,
},
];
}
// use the infinite query as usual
const { data, status } = useInfiniteQuery(...getQueryArgs(filters));
// when you want to prefetch
queryCache.prefetchQuery(...getQueryArgs(nextFilters)); https://react-query.tanstack.com/docs/api#useinfinitequery P.S., Thanks so much for this fantastic library! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hi @bryanrsmith ,
|
Beta Was this translation helpful? Give feedback.
Hi @bryanrsmith ,
I think I'm using a newer version now, but in case someone will end up in the same discussion queryClient now has prefetchinfinitequery method. Which solves this problem.