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

QueryErrorResetBoundary.tsx

168 lines4.4 KB

1import { createContext } from 'preact'
2import type { ComponentChildren } from 'preact'
3import { useContext, useState } from 'preact/hooks'
4​
5// CONTEXT
6​
7/**
8 * Resets any query errors within the boundary, so queries know they can try again.
9 */
10export type QueryErrorResetFunction = () => void
11​
12/**
13 * Returns whether the boundary has been reset and not yet cleared.
14 */
15export type QueryErrorIsResetFunction = () => boolean
16​
17/**
18 * Clears the reset state, so queries know not to try again until the boundary is reset again.
19 */
20export type QueryErrorClearResetFunction = () => void
21​
22export interface QueryErrorResetBoundaryValue {
23 /**
24 * Clears the reset state, so queries know not to try again until the boundary is reset again.
25 */
26 clearReset: QueryErrorClearResetFunction
27 /**
28 * Returns whether the boundary has been reset and not yet cleared.
29 */
30 isReset: QueryErrorIsResetFunction
31 /**
32 * Resets any query errors within the boundary, so queries know they can try again.
33 */
34 reset: QueryErrorResetFunction
36​
37function createValue(): QueryErrorResetBoundaryValue {
38 let isReset = false
39 return {
40 clearReset: () => {
41 isReset = false
42 },
43 reset: () => {
44 isReset = true
45 },
46 isReset: () => {
47 return isReset
48 },
49 }
51​
52const QueryErrorResetBoundaryContext = createContext(createValue())
53​
54// HOOK
55​
56/**
57 * This hook will reset any query errors within the closest `QueryErrorResetBoundary`. If there is no boundary
58 * defined it will reset them globally.
59 *
60 * @returns The boundary's {@link QueryErrorResetBoundaryValue}.
61 *
62 * @example
63 * ```tsx
64 * import { useErrorBoundary } from 'preact/hooks'
65 * import type { ComponentChildren } from 'preact'
66 * import { useQueryErrorResetBoundary } from '@tanstack/preact-query'
67 *
68 * function App({ children }: { children: ComponentChildren }) {
69 * const { reset } = useQueryErrorResetBoundary()
70 * const [error, resetError] = useErrorBoundary(() => reset())
71 *
72 * if (error) {
73 * return (
74 * <div>
75 * There was an error!
76 * <button onClick={() => resetError()}>Try again</button>
77 * </div>
78 * )
79 * }
80 *
81 * return children
82 * }
83 * ```
84 */
85export const useQueryErrorResetBoundary = () =>
86 useContext(QueryErrorResetBoundaryContext)
87​
88// COMPONENT
89​
90/**
91 * A render-prop function usable as `children` on `QueryErrorResetBoundary`.
92 *
93 * @param value - The boundary's {@link QueryErrorResetBoundaryValue}.
94 * @returns The children to render.
95 */
96export type QueryErrorResetBoundaryFunction = (
97 value: QueryErrorResetBoundaryValue,
98) => ComponentChildren
99​
100/**
101 * The props accepted by `QueryErrorResetBoundary`.
102 */
103export interface QueryErrorResetBoundaryProps {
104 /**
105 * Either a plain node, or a function that receives the boundary's {@link QueryErrorResetBoundaryValue} and
106 * returns a node.
107 */
108 children: QueryErrorResetBoundaryFunction | ComponentChildren
110​
111/**
112 * When using `suspense` or `throwOnError` in your queries, you need a way to let queries know that you want to
113 * try again when re-rendering after some error occurred. With the `QueryErrorResetBoundary` component you can
114 * reset any query errors within the boundaries of the component.
115 *
116 * @returns The `children`, rendered as-is, or called with the boundary's {@link QueryErrorResetBoundaryValue}
117 * if `children` is a function.
118 *
119 * @example
120 * ```tsx
121 * import { useErrorBoundary } from 'preact/hooks'
122 * import type { ComponentChildren } from 'preact'
123 * import { QueryErrorResetBoundary } from '@tanstack/preact-query'
124 *
125 * function ErrorBoundary({
126 * children,
127 * reset,
128 * }: {
129 * children: ComponentChildren
130 * reset: () => void
131 * }) {
132 * const [error, resetError] = useErrorBoundary(() => reset())
133 *
134 * if (error) {
135 * return (
136 * <div>
137 * There was an error!
138 * <button onClick={() => resetError()}>Try again</button>
139 * </div>
140 * )
141 * }
142 *
143 * return children
144 * }
145 *
146 * function App() {
147 * return (
148 * <QueryErrorResetBoundary>
149 * {({ reset }) => (
150 * <ErrorBoundary reset={reset}>
151 * <Page />
152 * </ErrorBoundary>
153 * )}
154 * </QueryErrorResetBoundary>
155 * )
156 * }
157 * ```
158 */
159export const QueryErrorResetBoundary = ({
160 children,
161}: QueryErrorResetBoundaryProps) => {
162 const [value] = useState(() => createValue())
163 return (
164 <QueryErrorResetBoundaryContext.Provider value={value}>
165 {typeof children === 'function' ? children(value) : children}
166 </QueryErrorResetBoundaryContext.Provider>
167 )