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

usePrefetchInfiniteQuery.tsx

77 lines2.8 KB

1import { noop } from '@tanstack/query-core'
2import { useQueryClient } from './QueryClientProvider'
3​
4import type {
5 DefaultError,
6 InfiniteData,
7 QueryClient,
8 QueryKey,
9} from '@tanstack/query-core'
10import type { UsePrefetchInfiniteQueryOptions } from './types'
11​
12/**
13 * `usePrefetchInfiniteQuery` does not return anything, it should be used just to fire a prefetch during render,
14 * before a suspense boundary that wraps a component that uses `useSuspenseInfiniteQuery`. You can pass
15 * everything to `usePrefetchInfiniteQuery` that you can pass to `queryClient.infiniteQuery`, though
16 * `queryKey`, `initialPageParam`, and `getNextPageParam` are always required, and `queryFn` is required unless
17 * a default query function has been defined.
18 *
19 * `getNextPageParam` receives both the last page of the infinite list of data and the full array of all pages,
20 * as well as pageParam information, and should return a single variable that will be passed to your query
21 * function as `context.pageParam`. Return `undefined` or `null` to indicate there is no next page available.
22 *
23 * The prefetch is skipped if the query already has any cached state β€” including a `pending`/`error` state left
24 * over from a previous attempt β€” so calling this on every render is cheap and won't refetch data that's
25 * already there or already in flight.
26 *
27 * @param options - The {@link UsePrefetchInfiniteQueryOptions} to use β€” everything you can pass to `queryClient.infiniteQuery`.
28 * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
29 * be used.
30 * @returns `void` β€” nothing is returned.
31 *
32 * @example
33 * ```tsx
34 * import { Suspense } from 'react'
35 * import { infiniteQueryOptions, usePrefetchInfiniteQuery } from '@tanstack/react-query'
36 *
37 * const projectsOptions = infiniteQueryOptions({
38 * queryKey: ['projects'],
39 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
40 * initialPageParam: 0,
41 * getNextPageParam: (lastPage) => lastPage.nextId,
42 * })
43 *
44 * function App() {
45 * // Fire the prefetch during render, before the suspense boundary below.
46 * usePrefetchInfiniteQuery(projectsOptions)
47 *
48 * return (
49 * <Suspense fallback={<h1>Loading projects...</h1>}>
50 * <Projects />
51 * </Suspense>
52 * )
53 * }
54 * ```
55 */
56export function usePrefetchInfiniteQuery<
57 TQueryFnData = unknown,
58 TError = DefaultError,
59 TData = InfiniteData<TQueryFnData>,
60 TQueryKey extends QueryKey = QueryKey,
61 TPageParam = unknown,
62>(
63 options: UsePrefetchInfiniteQueryOptions<
64 TQueryFnData,
65 TError,
66 TData,
67 TQueryKey,
68 TPageParam
69 >,
70 queryClient?: QueryClient,
71): void {
72 const client = useQueryClient(queryClient)
73​
74 if (!client.getQueryState(options.queryKey)) {
75 void client.infiniteQuery(options).catch(noop)
76 }