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

suspense.md

87 lines2.2 KB

1---
2id: suspense
3title: Suspense
4ref: docs/framework/react/guides/suspense.md
5replace: { 'react-query': 'preact-query', 'React': 'Preact' }
6---
7​
8[//]: # 'Info'
9​
10When using suspense mode, `status` states and `error` objects are not needed and are then replaced by usage of the `Suspense` component from `preact/compat` (including the use of the `fallback` prop and error boundaries for catching errors). Please read the [Resetting Error Boundaries](#resetting-error-boundaries) section for more information on how to set up suspense mode.
11​
12[//]: # 'Info'
13[//]: # 'PlaceholderData'
14​
15`placeholderData` also doesn't exist for this Query.
16​
17[//]: # 'PlaceholderData'
18[//]: # 'ExampleResetComponent'
19​
20Preact does not ship an error boundary component, but one can be built with the [`useErrorBoundary`](https://preactjs.com/guide/v10/hooks/#useerrorboundary) hook:
21​
22```tsx
23import { QueryErrorResetBoundary } from '@tanstack/preact-query'
24import { useErrorBoundary } from 'preact/hooks'
25​
26function ErrorBoundary({ children, onReset, fallbackRender }) {
27 const [error, resetError] = useErrorBoundary()
28​
29 if (error) {
30 return fallbackRender({
31 error,
32 resetErrorBoundary: () => {
33 onReset()
34 resetError()
35 },
36 })
37 }
38​
39 return children
41​
42const App = () => (
43 <QueryErrorResetBoundary>
44 {({ reset }) => (
45 <ErrorBoundary
46 onReset={reset}
47 fallbackRender={({ resetErrorBoundary }) => (
48 <div>
49 There was an error!
50 <button onClick={() => resetErrorBoundary()}>Try again</button>
51 </div>
52 )}
53 >
54 <Page />
55 </ErrorBoundary>
56 )}
57 </QueryErrorResetBoundary>
59```
60​
61[//]: # 'ExampleResetComponent'
62[//]: # 'ExampleResetHook'
63​
64```tsx
65import { useQueryErrorResetBoundary } from '@tanstack/preact-query'
66​
67const App = () => {
68 const { reset } = useQueryErrorResetBoundary()
69 return (
70 <ErrorBoundary
71 onReset={reset}
72 fallbackRender={({ resetErrorBoundary }) => (
73 <div>
74 There was an error!
75 <button onClick={() => resetErrorBoundary()}>Try again</button>
76 </div>
77 )}
78 >
79 <Page />
80 </ErrorBoundary>
81 )
83```
84​
85[//]: # 'ExampleResetHook'
86[//]: # 'Streaming'
87[//]: # 'Streaming'