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