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

QueryErrorResetBoundary.tsx

180 lines5.1 KB

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