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

usePrefetchInfiniteQuery.md

90 lines2.8 KB

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