forked from TanStack/query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReactQueryErrorResetBoundary.tsx
51 lines (42 loc) · 1.03 KB
/
ReactQueryErrorResetBoundary.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import React from 'react'
// CONTEXT
interface ReactQueryErrorResetBoundaryValue {
clearReset: () => void
isReset: () => boolean
reset: () => void
}
function createValue(): ReactQueryErrorResetBoundaryValue {
let isReset = true
return {
clearReset: () => {
isReset = false
},
reset: () => {
isReset = true
},
isReset: () => {
return isReset
},
}
}
const context = React.createContext(createValue())
// HOOK
export const useErrorResetBoundary = () => React.useContext(context)
// COMPONENT
export interface ReactQueryErrorResetBoundaryProps {
children:
| ((value: ReactQueryErrorResetBoundaryValue) => React.ReactNode)
| React.ReactNode
}
export const ReactQueryErrorResetBoundary: React.FC<ReactQueryErrorResetBoundaryProps> = ({
children,
}) => {
const value = React.useMemo(() => createValue(), [])
return (
<context.Provider value={value}>
{typeof children === 'function'
? (children as Function)(value)
: children}
</context.Provider>
)
}