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