Open History to see the last commit
1---
2id: useSuspenseQuery
3title: useSuspenseQuery
4redirect_from:
5 - framework/react/reference/useSuspenseQuery
6---
7β
8```ts
9function useSuspenseQuery<TQueryFnData, TError, TData, TQueryKey>(options: UseSuspenseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: QueryClient): UseSuspenseQueryResult<TData, TError>;
10```
11β
12Defined in: [packages/react-query/src/useSuspenseQuery.ts:75](https://github.com/TanStack/query/blob/main/packages/react-query/src/useSuspenseQuery.ts#L75)
13β
14The options for `useSuspenseQuery` are the same as for `useQuery`, except for `throwOnError`, `enabled`, and
15`placeholderData` β and `queryFn` may not be `skipToken`, since Suspense hooks can't render a "disabled" state.
16β
17Caveat: cancellation does not work.
18β
19## Type Parameters
20β
21### TQueryFnData
22β
23`TQueryFnData` = `unknown`
24β
25### TError
26β
27`TError` = `Error`
28β
29### TData
30β
31`TData` = `TQueryFnData`
32β
33### TQueryKey
34β
35`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
36β
37## Parameters
38β
39### options
40β
41[`UseSuspenseQueryOptions`](../interfaces/UseSuspenseQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
42β
43The [UseSuspenseQueryOptions](../interfaces/UseSuspenseQueryOptions.md) to use β the same options as `useQuery`, minus the ones listed above.
44β
45### queryClient?
46β
47[`QueryClient`](../classes/QueryClient.md)
48β
49Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
50be used.
51β
52## Returns
53β
54[`UseSuspenseQueryResult`](../type-aliases/UseSuspenseQueryResult.md)\<`TData`, `TError`\>
55β
56The same object as `useQuery`, except that `data` is guaranteed to be defined, `isPlaceholderData`
57is missing, and `status` is either `success` or `error` (with the derived flags set accordingly).
58β
59## Remarks
60β
61Multiple `useSuspenseQuery` calls in the same component suspend serially, causing a request
62waterfall β each one blocks rendering until it resolves, so the next doesn't even start fetching until then.
63Use [useSuspenseQueries](useSuspenseQueries.md) instead when you have more than one suspenseful query in a component, so they
64fetch in parallel.
65β
66## Example
67β
68The query error is thrown if the fetch fails and no cached data exists yet, so an error boundary is
69required around `<Suspense>`. A failed background refetch instead continues to render the cached data.
70Use [QueryErrorResetBoundary](QueryErrorResetBoundary.md) to let the user retry after such an error:
71```tsx
72import { Suspense } from 'react'
73import { ErrorBoundary } from 'react-error-boundary'
74import { QueryErrorResetBoundary, useSuspenseQuery } from '@tanstack/react-query'
75β
76function Posts() {
77 // `data` is guaranteed to be defined here β no `isPending` check needed.
78 const { data, isFetching } = useSuspenseQuery({
79 queryKey: ['posts'],
80 queryFn: fetchPosts,
81 })
82β
83 return (
84 <div>
85 <h1>Posts {isFetching ? '(refreshing...)' : null}</h1>
86 <ul>
87 {data.map((post) => (
88 <li key={post.id}>{post.title}</li>
89 ))}
90 </ul>
91 </div>
92 )
93}
94β
95function App() {
96 return (
97 <QueryErrorResetBoundary>
98 {({ reset }) => (
99 <ErrorBoundary
100 onReset={reset}
101 fallbackRender={({ resetErrorBoundary }) => (
102 <div>
103 There was an error!
104 <button onClick={() => resetErrorBoundary()}>Try again</button>
105 </div>
106 )}
107 >
108 <Suspense fallback={<h1>Loading posts...</h1>}>
109 <Posts />
110 </Suspense>
111 </ErrorBoundary>
112 )}
113 </QueryErrorResetBoundary>
114 )
115}
116```