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