Open History to see the last commit
1---
2id: useSuspenseQueries
3title: useSuspenseQueries
4redirect_from:
5 - framework/react/reference/useSuspenseQueries
6---
7
8## Overview
9
10```ts
11function useSuspenseQueries<T, TCombinedResult>(options: object, queryClient?: QueryClient): TCombinedResult;
12function useSuspenseQueries<T, TCombinedResult>(options: object, queryClient?: QueryClient): TCombinedResult;
13```
14
15- [`{ queries, combine }` → `TCombinedResult`](#call-signature-1): The options for `useSuspenseQueries` are the same as for `useQueries`, except that the top-level `subscribed` option isn't supported, and each `query` can't have `throwOnError`, `enabled`, or `placeholderData`.
16- [`{ queries, combine }` → `TCombinedResult`](#call-signature-2): The options for `useSuspenseQueries` are the same as for `useQueries`, except that the top-level `subscribed` option isn't supported, and each `query` can't have `throwOnError`, `enabled`, or `placeholderData`.
17
18See also: [Parameters](#parameters-summary) · [Returns](#returns-summary)
19
20<a id="call-signature-1"></a>
21
22## Call Signature
23
24```ts
25function useSuspenseQueries<T, TCombinedResult>(options: object, queryClient?: QueryClient): TCombinedResult;
26```
27
28Defined in: [packages/react-query/src/useSuspenseQueries.ts:347](https://github.com/TanStack/query/blob/main/packages/react-query/src/useSuspenseQueries.ts#L347)
29
30The options for `useSuspenseQueries` are the same as for `useQueries`, except that the top-level `subscribed`
31option isn't supported, and each `query` can't have `throwOnError`, `enabled`, or `placeholderData`.
32
33### Type Parameters
34
35#### T
36
37`T` *extends* `any`[]
38
39#### TCombinedResult
40
41`TCombinedResult` = `T` *extends* \[\] ? \[\] : `T` *extends* \[`Head`\] ? \[`GetUseSuspenseQueryResult`\<`Head`\>\] : `T` *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...Tails[]`\] *extends* \[`Head`\] ? \[`GetUseSuspenseQueryResult`\<`Head`\>, `GetUseSuspenseQueryResult`\<`Head`\>\] : \[`...Tails[]`\] *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...Tails[]`\] *extends* \[`Head`\] ? \[`GetUseSuspenseQueryResult`\<`Head`\>, `GetUseSuspenseQueryResult`\<`Head`\>, `GetUseSuspenseQueryResult`\<`Head`\>\] : \[`...Tails[]`\] *extends* \[`Head`, `...Tails[]`\] ? \[`...(...)[]`\] *extends* \[\] ? \[\] : ... *extends* ... ? ... : ... : \[`...{ [K in (...)]: (...) }[]`\] : \[...\{ \[K in string \| number \| symbol\]: GetUseSuspenseQueryResult\<Tails\[K\]\> \}\[\]\] : \{ \[K in string \| number \| symbol\]: GetUseSuspenseQueryResult\<T\[K\]\> \}
42
43### Parameters
44
45#### options
46
47The `queries` array to run in Suspense, and an optional `combine` function.
48
49##### combine?
50
51(`result`: `T` *extends* \[\] ? \[\] : `T` *extends* \[`Head`\] ? \[`GetUseSuspenseQueryResult`\<`Head`\>\] : `T` *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...Tails[]`\] *extends* \[`Head`\] ? \[`GetUseSuspenseQueryResult`\<`Head`\>, `GetUseSuspenseQueryResult`\<`Head`\>\] : \[`...Tails[]`\] *extends* \[`Head`, `...Tails[]`\] ? \[`...(...)[]`\] *extends* \[\] ? \[\] : ... *extends* ... ? ... : ... : \[`...{ [K in (...)]: (...) }[]`\] : \{ \[K in string \| number \| symbol\]: GetUseSuspenseQueryResult\<T\[K\]\> \}) => `TCombinedResult`
52
53Use this to combine the results of the queries into a single value. The result will be structurally
54shared to be as referentially stable as possible.
55
56##### queries
57
58 \| readonly \[`T` *extends* \[\] ? \[\] : `T` *extends* \[`Head`\] ? \[`GetUseSuspenseQueryOptions`\<`Head`\>\] : `T` *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...Tails[]`\] *extends* \[`Head`\] ? \[`GetUseSuspenseQueryOptions`\<`Head`\>, `GetUseSuspenseQueryOptions`\<`Head`\>\] : \[`...Tails[]`\] *extends* \[`Head`, `...Tails[]`\] ? \[`...(...)[]`\] *extends* \[\] ? \[\] : ... *extends* ... ? ... : ... : ...[] *extends* \[`...(...)[]`\] ? \[`...(...)[]`\] : ... *extends* ... ? ... : ... : `unknown`[] *extends* `T` ? `T` : `T` *extends* [`UseSuspenseQueryOptions`](../interfaces/UseSuspenseQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>[] ? [`UseSuspenseQueryOptions`](../interfaces/UseSuspenseQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>[] : [`UseSuspenseQueryOptions`](../interfaces/UseSuspenseQueryOptions.md)\<`unknown`, `Error`, `unknown`, readonly ...[]\>[]\]
59 \| readonly \[\{ \[K in string \| number \| symbol\]: GetUseSuspenseQueryOptions\<T\[K\]\> \}\]
60
61An array with query option objects identical to `useSuspenseQuery`.
62
63#### queryClient?
64
65[`QueryClient`](../classes/QueryClient.md)
66
67Use this to provide a custom `QueryClient`. Otherwise, the one from the nearest context
68will be used.
69
70### Returns
71
72`TCombinedResult`
73
74The same structure as `useQueries`, except that for each `query`, `data` is guaranteed to be
75defined, `isPlaceholderData` is missing, and `status` is either `success` or `error` (with the derived
76flags set accordingly).
77
78Caveat: the component will only re-mount after all queries have finished loading. Hence, if a query has gone
79stale in the time it took for all the queries to complete, it will be fetched again at re-mount. To avoid
80this, make sure to set a high enough `staleTime`. Cancellation does not work.
81
82### Examples
83
84The query error is thrown if a fetch fails and no cached data exists yet, so an error boundary is
85required around `<Suspense>`. A failed background refetch instead continues to render the cached data.
86Use [QueryErrorResetBoundary](QueryErrorResetBoundary.md) to let the user retry after such an error:
87```tsx
88import { Suspense } from 'react'
89import { ErrorBoundary } from 'react-error-boundary'
90import {
91 QueryErrorResetBoundary,
92 useSuspenseQueries,
93} from '@tanstack/react-query'
94
95function Posts({ ids }: { ids: Array<number> }) {
96 // Every result is guaranteed to be defined — no per-query `isPending` check needed.
97 const postQueries = useSuspenseQueries({
98 queries: ids.map((id) => ({
99 queryKey: ['post', id],
100 queryFn: () => fetchPost(id),
101 })),
102 })
103
104 return (
105 <ul>
106 {postQueries.map((query) => (
107 <li key={query.data.id}>{query.data.title}</li>
108 ))}
109 </ul>
110 )
111}
112
113function App() {
114 return (
115 <QueryErrorResetBoundary>
116 {({ reset }) => (
117 <ErrorBoundary
118 onReset={reset}
119 fallbackRender={({ resetErrorBoundary }) => (
120 <div>
121 There was an error!
122 <button onClick={() => resetErrorBoundary()}>Try again</button>
123 </div>
124 )}
125 >
126 <Suspense fallback={<h1>Loading posts...</h1>}>
127 <Posts ids={[1, 2, 3]} />
128 </Suspense>
129 </ErrorBoundary>
130 )}
131 </QueryErrorResetBoundary>
132 )
133}
134```
135
136Several different queries — use `useSuspenseQueries` instead of multiple `useSuspenseQuery` calls, so
137they fetch in parallel rather than suspending one after another:
138```tsx
139import { Suspense } from 'react'
140import { ErrorBoundary } from 'react-error-boundary'
141import {
142 QueryErrorResetBoundary,
143 useSuspenseQueries,
144} from '@tanstack/react-query'
145
146function Dashboard() {
147 const [usersQuery, teamsQuery, projectsQuery] = useSuspenseQueries({
148 queries: [
149 { queryKey: ['users'], queryFn: fetchUsers },
150 { queryKey: ['teams'], queryFn: fetchTeams },
151 { queryKey: ['projects'], queryFn: fetchProjects },
152 ],
153 })
154
155 return (
156 <div>
157 <UserList users={usersQuery.data} />
158 <TeamList teams={teamsQuery.data} />
159 <ProjectList projects={projectsQuery.data} />
160 </div>
161 )
162}
163
164function App() {
165 return (
166 <QueryErrorResetBoundary>
167 {({ reset }) => (
168 <ErrorBoundary
169 onReset={reset}
170 fallbackRender={({ resetErrorBoundary }) => (
171 <div>
172 There was an error!
173 <button onClick={() => resetErrorBoundary()}>Try again</button>
174 </div>
175 )}
176 >
177 <Suspense fallback={<h1>Loading dashboard...</h1>}>
178 <Dashboard />
179 </Suspense>
180 </ErrorBoundary>
181 )}
182 </QueryErrorResetBoundary>
183 )
184}
185```
186
187`combine`s the results into a single boolean, so `Refresh` only re-renders when that boolean changes,
188not on every individual query update. This overload is the only one that accepts `combine`:
189```tsx
190import { Suspense } from 'react'
191import { ErrorBoundary } from 'react-error-boundary'
192import {
193 QueryErrorResetBoundary,
194 useSuspenseQueries,
195} from '@tanstack/react-query'
196
197function Refresh() {
198 const anyFetching = useSuspenseQueries({
199 queries: [
200 { queryKey: ['users'], queryFn: fetchUsers },
201 { queryKey: ['teams'], queryFn: fetchTeams },
202 ],
203 combine: (results) => results.some((result) => result.isFetching),
204 })
205
206 return anyFetching ? <span>Refreshing…</span> : null
207}
208
209function App() {
210 return (
211 <QueryErrorResetBoundary>
212 {({ reset }) => (
213 <ErrorBoundary
214 onReset={reset}
215 fallbackRender={({ resetErrorBoundary }) => (
216 <div>
217 There was an error!
218 <button onClick={() => resetErrorBoundary()}>Try again</button>
219 </div>
220 )}
221 >
222 <Suspense fallback={<h1>Loading dashboard...</h1>}>
223 <Refresh />
224 </Suspense>
225 </ErrorBoundary>
226 )}
227 </QueryErrorResetBoundary>
228 )
229}
230```
231
232<a id="call-signature-2"></a>
233
234## Call Signature
235
236```ts
237function useSuspenseQueries<T, TCombinedResult>(options: object, queryClient?: QueryClient): TCombinedResult;
238```
239
240Defined in: [packages/react-query/src/useSuspenseQueries.ts:484](https://github.com/TanStack/query/blob/main/packages/react-query/src/useSuspenseQueries.ts#L484)
241
242The options for `useSuspenseQueries` are the same as for `useQueries`, except that the top-level `subscribed`
243option isn't supported, and each `query` can't have `throwOnError`, `enabled`, or `placeholderData`.
244
245### Type Parameters
246
247#### T
248
249`T` *extends* `any`[]
250
251#### TCombinedResult
252
253`TCombinedResult` = `T` *extends* \[\] ? \[\] : `T` *extends* \[`Head`\] ? \[`GetUseSuspenseQueryResult`\<`Head`\>\] : `T` *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...Tails[]`\] *extends* \[`Head`\] ? \[`GetUseSuspenseQueryResult`\<`Head`\>, `GetUseSuspenseQueryResult`\<`Head`\>\] : \[`...Tails[]`\] *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...Tails[]`\] *extends* \[`Head`\] ? \[`GetUseSuspenseQueryResult`\<`Head`\>, `GetUseSuspenseQueryResult`\<`Head`\>, `GetUseSuspenseQueryResult`\<`Head`\>\] : \[`...Tails[]`\] *extends* \[`Head`, `...Tails[]`\] ? \[`...(...)[]`\] *extends* \[\] ? \[\] : ... *extends* ... ? ... : ... : \[`...{ [K in (...)]: (...) }[]`\] : \[...\{ \[K in string \| number \| symbol\]: GetUseSuspenseQueryResult\<Tails\[K\]\> \}\[\]\] : \{ \[K in string \| number \| symbol\]: GetUseSuspenseQueryResult\<T\[K\]\> \}
254
255### Parameters
256
257#### options
258
259The `queries` array to run in Suspense, and an optional `combine` function.
260
261##### combine?
262
263(`result`: `T` *extends* \[\] ? \[\] : `T` *extends* \[`Head`\] ? \[`GetUseSuspenseQueryResult`\<`Head`\>\] : `T` *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...Tails[]`\] *extends* \[`Head`\] ? \[`GetUseSuspenseQueryResult`\<`Head`\>, `GetUseSuspenseQueryResult`\<`Head`\>\] : \[`...Tails[]`\] *extends* \[`Head`, `...Tails[]`\] ? \[`...(...)[]`\] *extends* \[\] ? \[\] : ... *extends* ... ? ... : ... : \[`...{ [K in (...)]: (...) }[]`\] : \{ \[K in string \| number \| symbol\]: GetUseSuspenseQueryResult\<T\[K\]\> \}) => `TCombinedResult`
264
265Use this to combine the results of the queries into a single value. The result will be structurally
266shared to be as referentially stable as possible.
267
268##### queries
269
270readonly \[`T` *extends* \[\] ? \[\] : `T` *extends* \[`Head`\] ? \[`GetUseSuspenseQueryOptions`\<`Head`\>\] : `T` *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...Tails[]`\] *extends* \[`Head`\] ? \[`GetUseSuspenseQueryOptions`\<`Head`\>, `GetUseSuspenseQueryOptions`\<`Head`\>\] : \[`...Tails[]`\] *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...(...)[]`\] *extends* \[...\] ? \[..., ..., ...\] : ... *extends* ... ? ... : ... : `unknown`[] *extends* \[`...Tails[]`\] ? \[`...Tails[]`\] : \[`...(...)[]`\] *extends* ...[] ? ...[] : ...[] : `unknown`[] *extends* `T` ? `T` : `T` *extends* [`UseSuspenseQueryOptions`](../interfaces/UseSuspenseQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>[] ? [`UseSuspenseQueryOptions`](../interfaces/UseSuspenseQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>[] : [`UseSuspenseQueryOptions`](../interfaces/UseSuspenseQueryOptions.md)\<`unknown`, `Error`, `unknown`, readonly `unknown`[]\>[]\]
271
272An array with query option objects identical to `useSuspenseQuery`.
273
274#### queryClient?
275
276[`QueryClient`](../classes/QueryClient.md)
277
278Use this to provide a custom `QueryClient`. Otherwise, the one from the nearest context
279will be used.
280
281### Returns
282
283`TCombinedResult`
284
285The same structure as `useQueries`, except that for each `query`, `data` is guaranteed to be
286defined, `isPlaceholderData` is missing, and `status` is either `success` or `error` (with the derived
287flags set accordingly).
288
289Caveat: the component will only re-mount after all queries have finished loading. Hence, if a query has gone
290stale in the time it took for all the queries to complete, it will be fetched again at re-mount. To avoid
291this, make sure to set a high enough `staleTime`. Cancellation does not work.
292
293### Examples
294
295The query error is thrown if a fetch fails and no cached data exists yet, so an error boundary is
296required around `<Suspense>`. A failed background refetch instead continues to render the cached data.
297Use [QueryErrorResetBoundary](QueryErrorResetBoundary.md) to let the user retry after such an error:
298```tsx
299import { Suspense } from 'react'
300import { ErrorBoundary } from 'react-error-boundary'
301import {
302 QueryErrorResetBoundary,
303 useSuspenseQueries,
304} from '@tanstack/react-query'
305
306function Posts({ ids }: { ids: Array<number> }) {
307 // Every result is guaranteed to be defined — no per-query `isPending` check needed.
308 const postQueries = useSuspenseQueries({
309 queries: ids.map((id) => ({
310 queryKey: ['post', id],
311 queryFn: () => fetchPost(id),
312 })),
313 })
314
315 return (
316 <ul>
317 {postQueries.map((query) => (
318 <li key={query.data.id}>{query.data.title}</li>
319 ))}
320 </ul>
321 )
322}
323
324function App() {
325 return (
326 <QueryErrorResetBoundary>
327 {({ reset }) => (
328 <ErrorBoundary
329 onReset={reset}
330 fallbackRender={({ resetErrorBoundary }) => (
331 <div>
332 There was an error!
333 <button onClick={() => resetErrorBoundary()}>Try again</button>
334 </div>
335 )}
336 >
337 <Suspense fallback={<h1>Loading posts...</h1>}>
338 <Posts ids={[1, 2, 3]} />
339 </Suspense>
340 </ErrorBoundary>
341 )}
342 </QueryErrorResetBoundary>
343 )
344}
345```
346
347Several different queries — use `useSuspenseQueries` instead of multiple `useSuspenseQuery` calls, so
348they fetch in parallel rather than suspending one after another:
349```tsx
350import { Suspense } from 'react'
351import { ErrorBoundary } from 'react-error-boundary'
352import {
353 QueryErrorResetBoundary,
354 useSuspenseQueries,
355} from '@tanstack/react-query'
356
357function Dashboard() {
358 const [usersQuery, teamsQuery, projectsQuery] = useSuspenseQueries({
359 queries: [
360 { queryKey: ['users'], queryFn: fetchUsers },
361 { queryKey: ['teams'], queryFn: fetchTeams },
362 { queryKey: ['projects'], queryFn: fetchProjects },
363 ],
364 })
365
366 return (
367 <div>
368 <UserList users={usersQuery.data} />
369 <TeamList teams={teamsQuery.data} />
370 <ProjectList projects={projectsQuery.data} />
371 </div>
372 )
373}
374
375function App() {
376 return (
377 <QueryErrorResetBoundary>
378 {({ reset }) => (
379 <ErrorBoundary
380 onReset={reset}
381 fallbackRender={({ resetErrorBoundary }) => (
382 <div>
383 There was an error!
384 <button onClick={() => resetErrorBoundary()}>Try again</button>
385 </div>
386 )}
387 >
388 <Suspense fallback={<h1>Loading dashboard...</h1>}>
389 <Dashboard />
390 </Suspense>
391 </ErrorBoundary>
392 )}
393 </QueryErrorResetBoundary>
394 )
395}
396```
397
398<a id="parameters-summary"></a>
399
400## Parameters
401
402### options
403
404The `queries` array to run in Suspense, and an optional `combine` function.
405
406#### combine?
407
408(`result`: `T` *extends* \[\] ? \[\] : `T` *extends* \[`Head`\] ? \[`GetUseSuspenseQueryResult`\<`Head`\>\] : `T` *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...Tails[]`\] *extends* \[`Head`\] ? \[`GetUseSuspenseQueryResult`\<`Head`\>, `GetUseSuspenseQueryResult`\<`Head`\>\] : \[`...Tails[]`\] *extends* \[`Head`, `...Tails[]`\] ? \[`...(...)[]`\] *extends* \[\] ? \[\] : ... *extends* ... ? ... : ... : \[`...{ [K in (...)]: (...) }[]`\] : \{ \[K in string \| number \| symbol\]: GetUseSuspenseQueryResult\<T\[K\]\> \}) => `TCombinedResult`
409
410Use this to combine the results of the queries into a single value. The result will be structurally
411shared to be as referentially stable as possible.
412
413#### queries
414
415readonly \[`T` *extends* \[\] ? \[\] : `T` *extends* \[`Head`\] ? \[`GetUseSuspenseQueryOptions`\<`Head`\>\] : `T` *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...Tails[]`\] *extends* \[`Head`\] ? \[`GetUseSuspenseQueryOptions`\<`Head`\>, `GetUseSuspenseQueryOptions`\<`Head`\>\] : \[`...Tails[]`\] *extends* \[`Head`, `...Tails[]`\] ? \[`...Tails[]`\] *extends* \[\] ? \[\] : \[`...(...)[]`\] *extends* \[...\] ? \[..., ..., ...\] : ... *extends* ... ? ... : ... : `unknown`[] *extends* \[`...Tails[]`\] ? \[`...Tails[]`\] : \[`...(...)[]`\] *extends* ...[] ? ...[] : ...[] : `unknown`[] *extends* `T` ? `T` : `T` *extends* [`UseSuspenseQueryOptions`](../interfaces/UseSuspenseQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>[] ? [`UseSuspenseQueryOptions`](../interfaces/UseSuspenseQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>[] : [`UseSuspenseQueryOptions`](../interfaces/UseSuspenseQueryOptions.md)\<`unknown`, `Error`, `unknown`, readonly `unknown`[]\>[]\]
416
417An array with query option objects identical to `useSuspenseQuery`.
418
419### queryClient?
420
421[`QueryClient`](../classes/QueryClient.md)
422
423Use this to provide a custom `QueryClient`. Otherwise, the one from the nearest context
424will be used.
425
426<a id="returns-summary"></a>
427
428## Returns
429
430`TCombinedResult`
431
432The same structure as `useQueries`, except that for each `query`, `data` is guaranteed to be
433defined, `isPlaceholderData` is missing, and `status` is either `success` or `error` (with the derived
434flags set accordingly).
435
436Caveat: the component will only re-mount after all queries have finished loading. Hence, if a query has gone
437stale in the time it took for all the queries to complete, it will be fetched again at re-mount. To avoid
438this, make sure to set a high enough `staleTime`. Cancellation does not work.