Open History to see the last commit
1---
2id: usePrefetchInfiniteQuery
3title: usePrefetchInfiniteQuery
4redirect_from:
5 - framework/react/reference/usePrefetchInfiniteQuery
6---
7β
8```ts
9function usePrefetchInfiniteQuery<TQueryFnData, TError, TData, TQueryKey, TPageParam>(options: UsePrefetchInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient?: QueryClient): void;
10```
11β
12Defined in: [packages/react-query/src/usePrefetchInfiniteQuery.tsx:53](https://github.com/TanStack/query/blob/main/packages/react-query/src/usePrefetchInfiniteQuery.tsx#L53)
13β
14`usePrefetchInfiniteQuery` does not return anything, it should be used just to fire a prefetch during render,
15before a suspense boundary that wraps a component that uses `useSuspenseInfiniteQuery`. You can pass
16everything to `usePrefetchInfiniteQuery` that you can pass to `queryClient.infiniteQuery`, though
17`queryKey`, `initialPageParam`, and `getNextPageParam` are always required, and `queryFn` is required unless
18a default query function has been defined.
19β
20`getNextPageParam` receives both the last page of the infinite list of data and the full array of all pages,
21as well as pageParam information, and should return a single variable that will be passed to your query
22function as `context.pageParam`. Return `undefined` or `null` to indicate there is no next page available.
23β
24The prefetch is skipped if the query already has any cached state β including a `pending`/`error` state left
25over from a previous attempt β so calling this on every render is cheap and won't refetch data that's
26already there or already in flight.
27β
28## Type Parameters
29β
30### TQueryFnData
31β
32`TQueryFnData` = `unknown`
33β
34### TError
35β
36`TError` = `Error`
37β
38### TData
39β
40`TData` = [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `unknown`\>
41β
42### TQueryKey
43β
44`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
45β
46### TPageParam
47β
48`TPageParam` = `unknown`
49β
50## Parameters
51β
52### options
53β
54[`UsePrefetchInfiniteQueryOptions`](../type-aliases/UsePrefetchInfiniteQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
55β
56The [UsePrefetchInfiniteQueryOptions](../type-aliases/UsePrefetchInfiniteQueryOptions.md) to use β everything you can pass to `queryClient.infiniteQuery`.
57β
58### queryClient?
59β
60[`QueryClient`](../classes/QueryClient.md)
61β
62Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
63be used.
64β
65## Returns
66β
67`void`
68β
69## Example
70β
71```tsx
72import { Suspense } from 'react'
73import { infiniteQueryOptions, usePrefetchInfiniteQuery } from '@tanstack/react-query'
74β
75const projectsOptions = infiniteQueryOptions({
76 queryKey: ['projects'],
77 queryFn: ({ pageParam }) => fetchProjects(pageParam),
78 initialPageParam: 0,
79 getNextPageParam: (lastPage) => lastPage.nextId,
80})
81β
82function App() {
83 // Fire the prefetch during render, before the suspense boundary below.
84 usePrefetchInfiniteQuery(projectsOptions)
85β
86 return (
87 <Suspense fallback={<h1>Loading projects...</h1>}>
88 <Projects />
89 </Suspense>
90 )
91}
92```