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

useSuspenseQuery.md

116 lines3.4 KB

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