Open History to see the last commit
1---
2id: suspense
3title: Suspense
4---
5β
6> NOTE: Suspense mode for React Query is experimental, same as Suspense for data fetching itself. These APIs WILL change and should not be used in production unless you lock both your React and React Query versions to patch-level versions that are compatible with each other.
7β
8React Query can also be used with React's new Suspense for Data Fetching API's. To enable this mode, you can set either the global or query level config's `suspense` option to `true`.
9β
10Global configuration:
11β
12```tsx
13// Configure for all queries
14import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
15β
16const queryClient = new QueryClient({
17 defaultOptions: {
18 queries: {
19 suspense: true,
20 },
21 },
22})
23β
24function Root() {
25 return (
26 <QueryClientProvider client={queryClient}>
27 <App />
28 </QueryClientProvider>
29 )
30}
31```
32β
33Query configuration:
34β
35```tsx
36import { useQuery } from '@tanstack/react-query'
37β
38// Enable for an individual query
39useQuery({ queryKey, queryFn, suspense: true })
40```
41β
42When using suspense mode, `status` states and `error` objects are not needed and are then replaced by usage of the `React.Suspense` component (including the use of the `fallback` prop and React error boundaries for catching errors). Please read the [Resetting Error Boundaries](#resetting-error-boundaries) and look at the [Suspense Example](https://codesandbox.io/s/github/tannerlinsley/react-query/tree/main/examples/react/suspense) for more information on how to set up suspense mode.
43β
44In addition to queries behaving differently in suspense mode, mutations also behave a bit differently. By default, instead of supplying the `error` variable when a mutation fails, it will be thrown during the next render of the component it's used in and propagate to the nearest error boundary, similar to query errors. If you wish to disable this, you can set the `throwOnError` option to `false`. If you wish that errors are not thrown at all, you can set the `throwOnError` option to `false` as well!
45β
46## Resetting Error Boundaries
47β
48Whether you are using **suspense** or **throwOnError** in your queries, you will need a way to let queries know that you want to try again when re-rendering after some error occurred.
49β
50Query errors can be reset with the `QueryErrorResetBoundary` component or with the `useQueryErrorResetBoundary` hook.
51β
52When using the component it will reset any query errors within the boundaries of the component:
53β
54```tsx
55import { QueryErrorResetBoundary } from '@tanstack/react-query'
56import { ErrorBoundary } from 'react-error-boundary'
57β
58const App: React.FC = () => (
59 <QueryErrorResetBoundary>
60 {({ reset }) => (
61 <ErrorBoundary
62 onReset={reset}
63 fallbackRender={({ resetErrorBoundary }) => (
64 <div>
65 There was an error!
66 <Button onClick={() => resetErrorBoundary()}>Try again</Button>
67 </div>
68 )}
69 >
70 <Page />
71 </ErrorBoundary>
72 )}
73 </QueryErrorResetBoundary>
74)
75```
76β
77When using the hook it will reset any query errors within the closest `QueryErrorResetBoundary`. If there is no boundary defined it will reset them globally:
78β
79```tsx
80import { useQueryErrorResetBoundary } from '@tanstack/react-query'
81import { ErrorBoundary } from 'react-error-boundary'
82β
83const App: React.FC = () => {
84 const { reset } = useQueryErrorResetBoundary()
85 return (
86 <ErrorBoundary
87 onReset={reset}
88 fallbackRender={({ resetErrorBoundary }) => (
89 <div>
90 There was an error!
91 <Button onClick={() => resetErrorBoundary()}>Try again</Button>
92 </div>
93 )}
94 >
95 <Page />
96 </ErrorBoundary>
97 )
98}
99```
100β
101## Fetch-on-render vs Render-as-you-fetch
102β
103Out of the box, React Query in `suspense` mode works really well as a **Fetch-on-render** solution with no additional configuration. This means that when your components attempt to mount, they will trigger query fetching and suspend, but only once you have imported them and mounted them. If you want to take it to the next level and implement a **Render-as-you-fetch** model, we recommend implementing [Prefetching](../guides/prefetching) on routing callbacks and/or user interactions events to start loading queries before they are mounted and hopefully even before you start importing or mounting their parent components.
104β
105## Further reading
106β
107For tips on using suspense option, check the [Suspensive React Query Package](../community/suspensive-react-query) from the Community Resources.