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