Open History to see the last commit
1import { InfiniteQueryObserver, skipToken } from '@tanstack/query-core'
2import type {
3 DefaultError,
4 InfiniteData,
5 InfiniteQueryObserverSuccessResult,
6 QueryClient,
7 QueryKey,
8 QueryObserver,
9} from '@tanstack/query-core'
10β
11import { defaultThrowOnError } from './suspense'
12import type {
13 UseSuspenseInfiniteQueryOptions,
14 UseSuspenseInfiniteQueryResult,
15} from './types'
16import { useBaseQuery } from './useBaseQuery'
17β
18/**
19 * The options for `useSuspenseInfiniteQuery` are the same as for `useInfiniteQuery`, except for `throwOnError`,
20 * `enabled`, and `placeholderData`.
21 *
22 * Caveat: cancellation does not work.
23 *
24 * @remarks Multiple suspenseful query calls in the same component suspend serially, causing a request
25 * waterfall β each one blocks rendering until it resolves, so the next doesn't even start fetching until
26 * then. There's no way to parallelize multiple infinite queries under Suspense. Also keep in mind that
27 * imperative fetch calls, such as `fetchNextPage`, may interfere with the default refetch behavior,
28 * resulting in outdated data. Make sure to call these functions only in response to user actions, or add
29 * conditions like `hasNextPage && !isFetching`.
30 * @see {@link useInfiniteQuery} for the non-Suspense version of this hook.
31 * @param options - The {@link UseSuspenseInfiniteQueryOptions} to use β the same options as `useInfiniteQuery`, minus the ones listed above.
32 * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
33 * be used.
34 * @returns The same object as `useInfiniteQuery`, except that `data` is guaranteed to be defined,
35 * `isPlaceholderData` is missing, and `status` is either `success` or `error` (with the derived flags set
36 * accordingly).
37 *
38 * @example
39 * ```tsx
40 * import { Suspense } from 'preact/compat'
41 * import { useSuspenseInfiniteQuery } from '@tanstack/preact-query'
42 *
43 * function Projects() {
44 * // `data` is guaranteed to be defined here β no `isPending` check needed.
45 * const { data, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage } =
46 * useSuspenseInfiniteQuery({
47 * queryKey: ['projects'],
48 * queryFn: ({ pageParam }) => fetchProjects(pageParam),
49 * initialPageParam: 0,
50 * getNextPageParam: (lastPage) => lastPage.nextId,
51 * })
52 *
53 * return (
54 * <div>
55 * <ul>
56 * {data.pages.map((page) =>
57 * page.projects.map((project) => <li key={project.id}>{project.name}</li>),
58 * )}
59 * </ul>
60 * <button
61 * onClick={() => fetchNextPage()}
62 * disabled={!hasNextPage || isFetching}
63 * >
64 * {isFetchingNextPage
65 * ? 'Loading more...'
66 * : hasNextPage
67 * ? 'Load More'
68 * : 'Nothing more to load'}
69 * </button>
70 * </div>
71 * )
72 * }
73 *
74 * function App() {
75 * return (
76 * <Suspense fallback={<h1>Loading projects...</h1>}>
77 * <Projects />
78 * </Suspense>
79 * )
80 * }
81 * ```
82 */
83export function useSuspenseInfiniteQuery<
84 TQueryFnData,
85 TError = DefaultError,
86 TData = InfiniteData<TQueryFnData>,
87 TQueryKey extends QueryKey = QueryKey,
88 TPageParam = unknown,
89>(
90 options: UseSuspenseInfiniteQueryOptions<
91 TQueryFnData,
92 TError,
93 TData,
94 TQueryKey,
95 TPageParam
96 >,
97 queryClient?: QueryClient,
98): UseSuspenseInfiniteQueryResult<TData, TError> {
99 if (process.env.NODE_ENV !== 'production') {
100 if ((options.queryFn as any) === skipToken) {
101 console.error('skipToken is not allowed for useSuspenseInfiniteQuery')
102 }
103 }
104β
105 return useBaseQuery(
106 {
107 ...options,
108 enabled: true,
109 suspense: true,
110 throwOnError: defaultThrowOnError,
111 placeholderData: undefined,
112 },
113 InfiniteQueryObserver as typeof QueryObserver,
114 queryClient,
115 ) as InfiniteQueryObserverSuccessResult<TData, TError>
116}