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