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

useSuspenseInfiniteQuery.md

142 lines4.3 KB

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