Open History to see the last commit
1---
2id: useSuspenseInfiniteQuery
3title: useSuspenseInfiniteQuery
4redirect_from:
5 - framework/react/reference/useSuspenseInfiniteQuery
6---
7β
8```ts
9function useSuspenseInfiniteQuery<TQueryFnData, TError, TData, TQueryKey, TPageParam>(options: UseSuspenseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient?: QueryClient): UseSuspenseInfiniteQueryResult<TData, TError>;
10```
11β
12Defined in: [packages/react-query/src/useSuspenseInfiniteQuery.ts:104](https://github.com/TanStack/query/blob/main/packages/react-query/src/useSuspenseInfiniteQuery.ts#L104)
13β
14The options for `useSuspenseInfiniteQuery` are the same as for `useInfiniteQuery`, except for `throwOnError`,
15`enabled`, and `placeholderData`.
16β
17Caveat: cancellation does not work.
18β
19## Type Parameters
20β
21### TQueryFnData
22β
23`TQueryFnData`
24β
25### TError
26β
27`TError` = `Error`
28β
29### TData
30β
31`TData` = [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `unknown`\>
32β
33### TQueryKey
34β
35`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
36β
37### TPageParam
38β
39`TPageParam` = `unknown`
40β
41## Parameters
42β
43### options
44β
45[`UseSuspenseInfiniteQueryOptions`](../interfaces/UseSuspenseInfiniteQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
46β
47The [UseSuspenseInfiniteQueryOptions](../interfaces/UseSuspenseInfiniteQueryOptions.md) to use β the same options as `useInfiniteQuery`, minus the ones listed above.
48β
49### queryClient?
50β
51[`QueryClient`](../classes/QueryClient.md)
52β
53Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
54be used.
55β
56## Returns
57β
58[`UseSuspenseInfiniteQueryResult`](../type-aliases/UseSuspenseInfiniteQueryResult.md)\<`TData`, `TError`\>
59β
60The same object as `useInfiniteQuery`, except that `data` is guaranteed to be defined,
61`isPlaceholderData` is missing, and `status` is either `success` or `error` (with the derived flags set
62accordingly).
63β
64## Remarks
65β
66Multiple suspenseful query calls in the same component suspend serially, causing a request
67waterfall β each one blocks rendering until it resolves, so the next doesn't even start fetching until
68then. There's no way to parallelize multiple infinite queries under Suspense. Also keep in mind that
69imperative fetch calls, such as `fetchNextPage`, may interfere with the default refetch behavior,
70resulting in outdated data. Make sure to call these functions only in response to user actions, or add
71conditions like `hasNextPage && !isFetching`.
72β
73## See
74β
75[useInfiniteQuery](useInfiniteQuery.md) for the non-Suspense version of this hook.
76β
77## Example
78β
79The query error is thrown if a fetch fails and no cached data exists yet, so an error boundary is
80required around `<Suspense>`. A failed background refetch instead continues to render the cached data.
81Use [QueryErrorResetBoundary](QueryErrorResetBoundary.md) to let the user retry after such an error:
82```tsx
83import { Suspense } from 'react'
84import { ErrorBoundary } from 'react-error-boundary'
85import {
86 QueryErrorResetBoundary,
87 useSuspenseInfiniteQuery,
88} from '@tanstack/react-query'
89β
90function Projects() {
91 // `data` is guaranteed to be defined here β no `isPending` check needed.
92 const { data, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage } =
93 useSuspenseInfiniteQuery({
94 queryKey: ['projects'],
95 queryFn: ({ pageParam }) => fetchProjects(pageParam),
96 initialPageParam: 0,
97 getNextPageParam: (lastPage) => lastPage.nextId,
98 })
99β
100 return (
101 <div>
102 <ul>
103 {data.pages.map((page) =>
104 page.projects.map((project) => <li key={project.id}>{project.name}</li>),
105 )}
106 </ul>
107 <button
108 onClick={() => fetchNextPage()}
109 disabled={!hasNextPage || isFetching}
110 >
111 {isFetchingNextPage
112 ? 'Loading more...'
113 : hasNextPage
114 ? 'Load More'
115 : 'Nothing more to load'}
116 </button>
117 </div>
118 )
119}
120β
121function App() {
122 return (
123 <QueryErrorResetBoundary>
124 {({ reset }) => (
125 <ErrorBoundary
126 onReset={reset}
127 fallbackRender={({ resetErrorBoundary }) => (
128 <div>
129 There was an error!
130 <button onClick={() => resetErrorBoundary()}>Try again</button>
131 </div>
132 )}
133 >
134 <Suspense fallback={<h1>Loading projects...</h1>}>
135 <Projects />
136 </Suspense>
137 </ErrorBoundary>
138 )}
139 </QueryErrorResetBoundary>
140 )
141}
142```