-
-
Notifications
You must be signed in to change notification settings - Fork 198
/
index.tsx
190 lines (162 loc) · 5.09 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import * as React from 'react'
const changedArray = (a: Array<unknown> = [], b: Array<unknown> = []) =>
a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]))
interface FallbackProps {
error: Error
resetErrorBoundary: (...args: Array<unknown>) => void
}
interface ErrorBoundaryPropsWithComponent {
onResetKeysChange?: (
prevResetKeys: Array<unknown> | undefined,
resetKeys: Array<unknown> | undefined,
) => void
onReset?: (...args: Array<unknown>) => void
onError?: (error: Error, info: {componentStack: string}) => void
resetKeys?: Array<unknown>
fallback?: never
FallbackComponent: React.ComponentType<FallbackProps>
fallbackRender?: never
}
declare function FallbackRender(
props: FallbackProps,
): React.ReactElement<
unknown,
string | React.FunctionComponent | typeof React.Component
> | null
interface ErrorBoundaryPropsWithRender {
onResetKeysChange?: (
prevResetKeys: Array<unknown> | undefined,
resetKeys: Array<unknown> | undefined,
) => void
onReset?: (...args: Array<unknown>) => void
onError?: (error: Error, info: {componentStack: string}) => void
resetKeys?: Array<unknown>
fallback?: never
FallbackComponent?: never
fallbackRender: typeof FallbackRender
}
interface ErrorBoundaryPropsWithFallback {
onResetKeysChange?: (
prevResetKeys: Array<unknown> | undefined,
resetKeys: Array<unknown> | undefined,
) => void
onReset?: (...args: Array<unknown>) => void
onError?: (error: Error, info: {componentStack: string}) => void
resetKeys?: Array<unknown>
fallback: React.ReactElement<
unknown,
string | React.FunctionComponent | typeof React.Component
> | null
FallbackComponent?: never
fallbackRender?: never
}
type ErrorBoundaryProps =
| ErrorBoundaryPropsWithFallback
| ErrorBoundaryPropsWithComponent
| ErrorBoundaryPropsWithRender
type ErrorBoundaryState = {error: Error | null}
const initialState: ErrorBoundaryState = {error: null}
class ErrorBoundary extends React.Component<
React.PropsWithRef<React.PropsWithChildren<ErrorBoundaryProps>>,
ErrorBoundaryState
> {
static getDerivedStateFromError(error: Error) {
return {error}
}
state = initialState
updatedWithError = false
resetErrorBoundary = (...args: Array<unknown>) => {
this.props.onReset?.(...args)
this.reset()
}
reset() {
this.updatedWithError = false
this.setState(initialState)
}
componentDidCatch(error: Error, info: React.ErrorInfo) {
this.props.onError?.(error, info)
}
componentDidMount() {
const {error} = this.state
if (error !== null) {
this.updatedWithError = true
}
}
componentDidUpdate(prevProps: ErrorBoundaryProps) {
const {error} = this.state
const {resetKeys} = this.props
// There's an edge case where if the thing that triggered the error
// happens to *also* be in the resetKeys array, we'd end up resetting
// the error boundary immediately. This would likely trigger a second
// error to be thrown.
// So we make sure that we don't check the resetKeys on the first call
// of cDU after the error is set
if (error !== null && !this.updatedWithError) {
this.updatedWithError = true
return
}
if (error !== null && changedArray(prevProps.resetKeys, resetKeys)) {
this.props.onResetKeysChange?.(prevProps.resetKeys, resetKeys)
this.reset()
}
}
render() {
const {error} = this.state
const {fallbackRender, FallbackComponent, fallback} = this.props
if (error !== null) {
const props = {
error,
resetErrorBoundary: this.resetErrorBoundary,
}
if (React.isValidElement(fallback)) {
return fallback
} else if (typeof fallbackRender === 'function') {
return fallbackRender(props)
} else if (FallbackComponent) {
return <FallbackComponent {...props} />
} else {
throw new Error(
'react-error-boundary requires either a fallback, fallbackRender, or FallbackComponent prop',
)
}
}
return this.props.children
}
}
function withErrorBoundary<P>(
Component: React.ComponentType<P>,
errorBoundaryProps: ErrorBoundaryProps,
): React.ComponentType<P> {
const Wrapped: React.ComponentType<P> = props => {
return (
<ErrorBoundary {...errorBoundaryProps}>
<Component {...props} />
</ErrorBoundary>
)
}
// Format for display in DevTools
const name = Component.displayName || Component.name || 'Unknown'
Wrapped.displayName = `withErrorBoundary(${name})`
return Wrapped
}
function useErrorHandler<P = Error>(
givenError?: P | null | undefined,
): React.Dispatch<React.SetStateAction<P | null>> {
const [error, setError] = React.useState<P | null>(null)
if (givenError) throw givenError
if (error) throw error
return setError
}
export {ErrorBoundary, withErrorBoundary, useErrorHandler}
export type {
FallbackProps,
ErrorBoundaryPropsWithComponent,
ErrorBoundaryPropsWithRender,
ErrorBoundaryPropsWithFallback,
ErrorBoundaryProps,
}
/*
eslint
@typescript-eslint/no-throw-literal: "off",
@typescript-eslint/prefer-nullish-coalescing: "off"
*/