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

QueryErrorResetBoundary.tsx

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