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
6export type QueryErrorResetFunction = () => void
7export type QueryErrorIsResetFunction = () => boolean
8export type QueryErrorClearResetFunction = () => void
9β
10export interface QueryErrorResetBoundaryValue {
11 clearReset: QueryErrorClearResetFunction
12 isReset: QueryErrorIsResetFunction
13 reset: QueryErrorResetFunction
14}
15β
16function createValue(): QueryErrorResetBoundaryValue {
17 let isReset = false
18 return {
19 clearReset: () => {
20 isReset = false
21 },
22 reset: () => {
23 isReset = true
24 },
25 isReset: () => {
26 return isReset
27 },
28 }
29}
30β
31const QueryErrorResetBoundaryContext = createContext(createValue())
32β
33// HOOK
34β
35export const useQueryErrorResetBoundary = () =>
36 useContext(QueryErrorResetBoundaryContext)
37β
38// COMPONENT
39β
40export type QueryErrorResetBoundaryFunction = (
41 value: QueryErrorResetBoundaryValue,
42) => ComponentChildren
43β
44export interface QueryErrorResetBoundaryProps {
45 children: QueryErrorResetBoundaryFunction | ComponentChildren
46}
47β
48export const QueryErrorResetBoundary = ({
49 children,
50}: QueryErrorResetBoundaryProps) => {
51 const [value] = useState(() => createValue())
52 return (
53 <QueryErrorResetBoundaryContext.Provider value={value}>
54 {typeof children === 'function' ? children(value) : children}
55 </QueryErrorResetBoundaryContext.Provider>
56 )
57}