Open History to see the last commit
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
35}
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 }
50}
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
109}
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 App() {
126 * return (
127 * <QueryErrorResetBoundary>
128 * {({ reset }) => (
129 * <ErrorBoundary reset={reset}>
130 * <Page />
131 * </ErrorBoundary>
132 * )}
133 * </QueryErrorResetBoundary>
134 * )
135 * }
136 *
137 * function ErrorBoundary({
138 * children,
139 * reset,
140 * }: {
141 * children: ComponentChildren
142 * reset: () => void
143 * }) {
144 * const [error, resetError] = useErrorBoundary(() => reset())
145 *
146 * if (error) {
147 * return (
148 * <div>
149 * There was an error!
150 * <button onClick={() => resetError()}>Try again</button>
151 * </div>
152 * )
153 * }
154 *
155 * return children
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 )
168}