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

suspense.md

103 lines4.3 KB

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 )
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 `useErrorBoundary` 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 **useErrorBoundaries** 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>
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 )
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.