Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

usePrefetchInfiniteQuery.md

92 lines2.9 KB

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 )
92```