react-error-boundary 3.1.3 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +94 -394
- package/dist/react-error-boundary.d.ts +65 -0
- package/dist/react-error-boundary.d.ts.map +1 -0
- package/dist/react-error-boundary.js +166 -0
- package/dist/react-error-boundary.js.map +1 -0
- package/dist/react-error-boundary.module.js +146 -0
- package/dist/react-error-boundary.module.js.map +1 -0
- package/package.json +28 -33
- package/CHANGELOG.md +0 -5
- package/dist/index.d.ts +0 -60
- package/dist/react-error-boundary.cjs.d.ts +0 -1
- package/dist/react-error-boundary.cjs.js +0 -187
- package/dist/react-error-boundary.esm.d.ts +0 -1
- package/dist/react-error-boundary.esm.js +0 -156
- package/dist/react-error-boundary.umd.d.ts +0 -1
- package/dist/react-error-boundary.umd.js +0 -203
- package/dist/react-error-boundary.umd.js.map +0 -1
- package/dist/react-error-boundary.umd.min.d.ts +0 -1
- package/dist/react-error-boundary.umd.min.js +0 -2
- package/dist/react-error-boundary.umd.min.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,442 +1,142 @@
|
|
|
1
|
-
|
|
2
|
-
<h1>react-error-boundary</h1>
|
|
1
|
+
# react-error-boundary
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
</div>
|
|
3
|
+
Reusable React [error boundary](https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary) component. Check out [this blog post](https://kentcdodds.com/blog/use-react-error-boundary-to-handle-errors-in-react) for examples of how this package can be used.
|
|
6
4
|
|
|
7
|
-
|
|
5
|
+
#### If you like this project, [buy me a coffee](http://givebrian.coffee/).
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
[![Build Status][build-badge]][build]
|
|
11
|
-
[![Code Coverage][coverage-badge]][coverage]
|
|
12
|
-
[![version][version-badge]][package]
|
|
13
|
-
[![downloads][downloads-badge]][npmtrends]
|
|
14
|
-
[![MIT License][license-badge]][license]
|
|
15
|
-
[![PRs Welcome][prs-badge]][prs]
|
|
16
|
-
[![Code of Conduct][coc-badge]][coc]
|
|
17
|
-
<!-- prettier-ignore-end -->
|
|
7
|
+
## Getting started
|
|
18
8
|
|
|
19
|
-
|
|
9
|
+
```sh
|
|
10
|
+
# npm
|
|
11
|
+
npm install react-error-boundary
|
|
20
12
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
## This solution
|
|
25
|
-
|
|
26
|
-
This component provides a simple and reusable wrapper that you can use to wrap
|
|
27
|
-
around your components. Any rendering errors in your components hierarchy can
|
|
28
|
-
then be gracefully handled.
|
|
29
|
-
|
|
30
|
-
Reading this blog post will help you understand what react-error-boundary does
|
|
31
|
-
for you:
|
|
32
|
-
[Use react-error-boundary to handle errors in React](https://kentcdodds.com/blog/use-react-error-boundary-to-handle-errors-in-react)
|
|
33
|
-
– How to simplify your React apps by handling React errors effectively with
|
|
34
|
-
react-error-boundary
|
|
35
|
-
|
|
36
|
-
## Table of Contents
|
|
37
|
-
|
|
38
|
-
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
39
|
-
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
|
40
|
-
|
|
41
|
-
- [Installation](#installation)
|
|
42
|
-
- [Usage](#usage)
|
|
43
|
-
- [Error Recovery](#error-recovery)
|
|
44
|
-
- [API](#api)
|
|
45
|
-
- [`ErrorBoundary` props](#errorboundary-props)
|
|
46
|
-
- [`useErrorHandler(error?: Error)`](#useerrorhandlererror-error)
|
|
47
|
-
- [Issues](#issues)
|
|
48
|
-
- [🐛 Bugs](#-bugs)
|
|
49
|
-
- [💡 Feature Requests](#-feature-requests)
|
|
50
|
-
- [LICENSE](#license)
|
|
13
|
+
# yarn
|
|
14
|
+
yarn add react-error-boundary
|
|
15
|
+
```
|
|
51
16
|
|
|
52
|
-
|
|
17
|
+
## API
|
|
53
18
|
|
|
54
|
-
|
|
19
|
+
### `ErrorBoundary` component
|
|
20
|
+
Wrap an `ErrorBoundary` around other React components to "catch" errors and render a fallback UI. The component supports several ways to render a fallback (shown below).
|
|
55
21
|
|
|
56
|
-
|
|
57
|
-
|
|
22
|
+
#### `ErrorBoundary` with `fallback` prop
|
|
23
|
+
The simplest way to render a default "something went wrong" type error message.
|
|
24
|
+
```js
|
|
25
|
+
import { ErrorBoundary } from "react-error-boundary";
|
|
58
26
|
|
|
27
|
+
<ErrorBoundary fallback={<div>Something went wrong</div>}>
|
|
28
|
+
<ExampleApplication />
|
|
29
|
+
</ErrorBoundary>
|
|
59
30
|
```
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
31
|
+
#### `ErrorBoundary` with `fallbackRender` prop
|
|
32
|
+
["Render prop"](https://react.dev/reference/react/Children#calling-a-render-prop-to-customize-rendering) function responsible for returning a fallback UI based on a thrown value.
|
|
33
|
+
```js
|
|
34
|
+
import { ErrorBoundary } from "react-error-boundary";
|
|
64
35
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
its descendants too.
|
|
36
|
+
function fallbackRender({ error, resetErrorBoundary }) {
|
|
37
|
+
// Call resetErrorBoundary() to reset the error boundary and retry the render.
|
|
68
38
|
|
|
69
|
-
```jsx
|
|
70
|
-
import {ErrorBoundary} from 'react-error-boundary'
|
|
71
|
-
|
|
72
|
-
function ErrorFallback({error, resetErrorBoundary}) {
|
|
73
39
|
return (
|
|
74
40
|
<div role="alert">
|
|
75
41
|
<p>Something went wrong:</p>
|
|
76
|
-
<pre>{error.message}</pre>
|
|
77
|
-
<button onClick={resetErrorBoundary}>Try again</button>
|
|
42
|
+
<pre style={{ color: "red" }}>{error.message}</pre>
|
|
78
43
|
</div>
|
|
79
|
-
)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const ui = (
|
|
83
|
-
<ErrorBoundary
|
|
84
|
-
FallbackComponent={ErrorFallback}
|
|
85
|
-
onReset={() => {
|
|
86
|
-
// reset the state of your app so the error doesn't happen again
|
|
87
|
-
}}
|
|
88
|
-
>
|
|
89
|
-
<ComponentThatMayError />
|
|
90
|
-
</ErrorBoundary>
|
|
91
|
-
)
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
You can react to errors (e.g. for logging) by providing an `onError` callback:
|
|
95
|
-
|
|
96
|
-
```jsx
|
|
97
|
-
import {ErrorBoundary} from 'react-error-boundary'
|
|
98
|
-
|
|
99
|
-
const myErrorHandler = (error: Error, info: {componentStack: string}) => {
|
|
100
|
-
// Do something with the error
|
|
101
|
-
// E.g. log to an error logging client here
|
|
44
|
+
);
|
|
102
45
|
}
|
|
103
46
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
[higher-order component](https://reactjs.org/docs/higher-order-components.html):
|
|
113
|
-
|
|
114
|
-
```jsx
|
|
115
|
-
import {withErrorBoundary} from 'react-error-boundary'
|
|
116
|
-
|
|
117
|
-
const ComponentWithErrorBoundary = withErrorBoundary(ComponentThatMayError, {
|
|
118
|
-
FallbackComponent: ErrorBoundaryFallbackComponent,
|
|
119
|
-
onError(error, info) {
|
|
120
|
-
// Do something with the error
|
|
121
|
-
// E.g. log to an error logging client here
|
|
122
|
-
},
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
const ui = <ComponentWithErrorBoundary />
|
|
47
|
+
<ErrorBoundary
|
|
48
|
+
fallbackRender={fallbackRender}
|
|
49
|
+
onReset={(details) => {
|
|
50
|
+
// Reset the state of your app so the error doesn't happen again
|
|
51
|
+
}}
|
|
52
|
+
>
|
|
53
|
+
<ExampleApplication />
|
|
54
|
+
</ErrorBoundary>;
|
|
126
55
|
```
|
|
56
|
+
#### `ErrorBoundary` with `FallbackComponent` prop
|
|
57
|
+
React component responsible for returning a fallback UI based on a thrown value.
|
|
58
|
+
```js
|
|
59
|
+
import { ErrorBoundary } from "react-error-boundary";
|
|
127
60
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
In the event of an error if you want to recover from that error and allow the
|
|
131
|
-
user to "try again" or continue with their work, you'll need a way to reset the
|
|
132
|
-
ErrorBoundary's internal state. You can do this various ways, but here's the
|
|
133
|
-
most idiomatic approach:
|
|
61
|
+
function Fallback({ error, resetErrorBoundary }) {
|
|
62
|
+
// Call resetErrorBoundary() to reset the error boundary and retry the render.
|
|
134
63
|
|
|
135
|
-
```jsx
|
|
136
|
-
function ErrorFallback({error, resetErrorBoundary}) {
|
|
137
64
|
return (
|
|
138
65
|
<div role="alert">
|
|
139
66
|
<p>Something went wrong:</p>
|
|
140
|
-
<pre>{error.message}</pre>
|
|
141
|
-
<button onClick={resetErrorBoundary}>Try again</button>
|
|
142
|
-
</div>
|
|
143
|
-
)
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function Bomb() {
|
|
147
|
-
throw new Error('💥 CABOOM 💥')
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
function App() {
|
|
151
|
-
const [explode, setExplode] = React.useState(false)
|
|
152
|
-
return (
|
|
153
|
-
<div>
|
|
154
|
-
<button onClick={() => setExplode(e => !e)}>toggle explode</button>
|
|
155
|
-
<ErrorBoundary
|
|
156
|
-
FallbackComponent={ErrorFallback}
|
|
157
|
-
onReset={() => setExplode(false)}
|
|
158
|
-
resetKeys={[explode]}
|
|
159
|
-
>
|
|
160
|
-
{explode ? <Bomb /> : null}
|
|
161
|
-
</ErrorBoundary>
|
|
67
|
+
<pre style={{ color: "red" }}>{error.message}</pre>
|
|
162
68
|
</div>
|
|
163
|
-
)
|
|
69
|
+
);
|
|
164
70
|
}
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
So, with this setup, you've got a button which when clicked will trigger an
|
|
168
|
-
error. Clicking the button again will trigger a re-render which recovers from
|
|
169
|
-
the error (we no longer render the `<Bomb />`). We also pass the `resetKeys`
|
|
170
|
-
prop which is an array of elements for the `ErrorBoundary` to check each render
|
|
171
|
-
(if there's currently an error state). If any of those elements change between
|
|
172
|
-
renders, then the `ErrorBoundary` will reset the state which will re-render the
|
|
173
|
-
children.
|
|
174
|
-
|
|
175
|
-
We have the `onReset` prop so that if the user clicks the "Try again" button we
|
|
176
|
-
have an opportunity to re-initialize our state into a good place before
|
|
177
|
-
attempting to re-render the children.
|
|
178
|
-
|
|
179
|
-
This combination allows us both the opportunity to give the user something
|
|
180
|
-
specific to do to recover from the error, and recover from the error by
|
|
181
|
-
interacting with other areas of the app that might fix things for us. It's hard
|
|
182
|
-
to describe here, but hopefully it makes sense when you apply it to your
|
|
183
|
-
specific scenario.
|
|
184
|
-
|
|
185
|
-
## API
|
|
186
|
-
|
|
187
|
-
### `ErrorBoundary` props
|
|
188
|
-
|
|
189
|
-
#### `children`
|
|
190
|
-
|
|
191
|
-
This is what you want rendered when everything's working fine. If there's an
|
|
192
|
-
error that React can handle within the children of the `ErrorBoundary`, the
|
|
193
|
-
`ErrorBoundary` will catch that and allow you to handle it gracefully.
|
|
194
|
-
|
|
195
|
-
#### `FallbackComponent`
|
|
196
|
-
|
|
197
|
-
This is a component you want rendered in the event of an error. As props it will
|
|
198
|
-
be passed the `error` and `resetErrorBoundary` (which will reset the error
|
|
199
|
-
boundary's state when called, useful for a "try again" button when used in
|
|
200
|
-
combination with the `onReset` prop).
|
|
201
|
-
|
|
202
|
-
This is required if no `fallback` or `fallbackRender` prop is provided.
|
|
203
|
-
|
|
204
|
-
#### `fallbackRender`
|
|
205
71
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
<ErrorBoundary
|
|
215
|
-
fallbackRender={({error, resetErrorBoundary}) => (
|
|
216
|
-
<div role="alert">
|
|
217
|
-
<div>Oh no</div>
|
|
218
|
-
<pre>{error.message}</pre>
|
|
219
|
-
<button
|
|
220
|
-
onClick={() => {
|
|
221
|
-
// this next line is why the fallbackRender is useful
|
|
222
|
-
resetComponentState()
|
|
223
|
-
// though you could accomplish this with a combination
|
|
224
|
-
// of the FallbackCallback and onReset props as well.
|
|
225
|
-
resetErrorBoundary()
|
|
226
|
-
}}
|
|
227
|
-
>
|
|
228
|
-
Try again
|
|
229
|
-
</button>
|
|
230
|
-
</div>
|
|
231
|
-
)}
|
|
232
|
-
>
|
|
233
|
-
<ComponentThatMayError />
|
|
234
|
-
</ErrorBoundary>
|
|
235
|
-
)
|
|
72
|
+
<ErrorBoundary
|
|
73
|
+
FallbackComponent={Fallback}
|
|
74
|
+
onReset={(details) => {
|
|
75
|
+
// Reset the state of your app so the error doesn't happen again
|
|
76
|
+
}}
|
|
77
|
+
>
|
|
78
|
+
<ExampleApplication />
|
|
79
|
+
</ErrorBoundary>;
|
|
236
80
|
```
|
|
237
81
|
|
|
238
|
-
|
|
239
|
-
around. Unfortunately, the current React Error Boundary API only supports class
|
|
240
|
-
components at the moment, so render props are the best solution we have to this
|
|
241
|
-
problem.
|
|
242
|
-
|
|
243
|
-
This is required if no `FallbackComponent` or `fallback` prop is provided.
|
|
82
|
+
#### Logging errors with `onError`
|
|
244
83
|
|
|
245
|
-
|
|
84
|
+
```js
|
|
85
|
+
import { ErrorBoundary } from "react-error-boundary";
|
|
246
86
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
though, so it's not really recommended.
|
|
87
|
+
const logError = (error: Error, info: { componentStack: string }) => {
|
|
88
|
+
// Do something with the error, e.g. log to an external API
|
|
89
|
+
};
|
|
251
90
|
|
|
252
|
-
```jsx
|
|
253
91
|
const ui = (
|
|
254
|
-
<ErrorBoundary
|
|
255
|
-
<
|
|
92
|
+
<ErrorBoundary FallbackComponent={ErrorFallback} onError={logError}>
|
|
93
|
+
<ExampleApplication />
|
|
256
94
|
</ErrorBoundary>
|
|
257
|
-
)
|
|
95
|
+
);
|
|
258
96
|
```
|
|
259
97
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
This will be called when there's been an error that the `ErrorBoundary` has
|
|
263
|
-
handled. It will be called with two arguments: `error`, `info`.
|
|
264
|
-
|
|
265
|
-
#### `onReset`
|
|
266
|
-
|
|
267
|
-
This will be called immediately before the `ErrorBoundary` resets it's internal
|
|
268
|
-
state (which will result in rendering the `children` again). You should use this
|
|
269
|
-
to ensure that re-rendering the children will not result in a repeat of the same
|
|
270
|
-
error happening again.
|
|
271
|
-
|
|
272
|
-
`onReset` will be called with whatever `resetErrorBoundary` is called with.
|
|
273
|
-
|
|
274
|
-
**Important**: `onReset` will _not_ be called when reset happens from a change
|
|
275
|
-
in `resetKeys`. Use `onResetKeysChange` for that.
|
|
276
|
-
|
|
277
|
-
#### `resetKeys`
|
|
278
|
-
|
|
279
|
-
Sometimes an error happens as a result of local state to the component that's
|
|
280
|
-
rendering the error. If this is the case, then you can pass `resetKeys` which is
|
|
281
|
-
an array of values. If the `ErrorBoundary` is in an error state, then it will
|
|
282
|
-
check these values each render and if they change from one render to the next,
|
|
283
|
-
then it will reset automatically (triggering a re-render of the `children`).
|
|
98
|
+
### `useErrorBoundary` hook
|
|
99
|
+
Convenience hook for imperatively showing or dismissing error boundaries.
|
|
284
100
|
|
|
285
|
-
|
|
101
|
+
React only handles errors thrown during render or during component lifecycle methods (e.g. effects and did-mount/did-update). Errors thrown in event handlers, or after async code has run, will not be caught.
|
|
286
102
|
|
|
287
|
-
|
|
103
|
+
This hook can be used to pass those errors to the nearest error boundary:
|
|
288
104
|
|
|
289
|
-
|
|
290
|
-
|
|
105
|
+
```js
|
|
106
|
+
import { useErrorBoundary } from "react-error-boundary";
|
|
291
107
|
|
|
292
|
-
|
|
108
|
+
function Example() {
|
|
109
|
+
const { resetBoundary, showErrorBoundary } = useErrorBoundary();
|
|
293
110
|
|
|
294
|
-
|
|
295
|
-
handle errors thrown during React's lifecycles. To quote
|
|
296
|
-
[the React docs on Error Boundaries](https://reactjs.org/docs/error-boundaries.html):
|
|
297
|
-
|
|
298
|
-
> Error boundaries do not catch errors for:
|
|
299
|
-
>
|
|
300
|
-
> - Event handlers
|
|
301
|
-
> ([learn more](https://reactjs.org/docs/error-boundaries.html#how-about-event-handlers))
|
|
302
|
-
> - Asynchronous code (e.g. setTimeout or requestAnimationFrame callbacks)
|
|
303
|
-
> - Server side rendering
|
|
304
|
-
> - Errors thrown in the error boundary itself (rather than its children)
|
|
305
|
-
|
|
306
|
-
This means you have to handle those errors yourself, but you probably would like
|
|
307
|
-
to reuse the error boundaries you worked hard on creating for those kinds of
|
|
308
|
-
errors as well. This is what `useErrorHandler` is for.
|
|
309
|
-
|
|
310
|
-
There are two ways to use `useErrorHandler`:
|
|
311
|
-
|
|
312
|
-
1. `const handleError = useErrorHandler()`: call `handleError(theError)`
|
|
313
|
-
2. `useErrorHandler(error)`: useful if you are managing the error state yourself
|
|
314
|
-
or get it from another hook.
|
|
315
|
-
|
|
316
|
-
Here's an example:
|
|
317
|
-
|
|
318
|
-
```javascript
|
|
319
|
-
function Greeting() {
|
|
320
|
-
const [greeting, setGreeting] = React.useState(null)
|
|
321
|
-
const handleError = useErrorHandler()
|
|
322
|
-
|
|
323
|
-
function handleSubmit(event) {
|
|
324
|
-
event.preventDefault()
|
|
325
|
-
const name = event.target.elements.name.value
|
|
111
|
+
useEffect(() => {
|
|
326
112
|
fetchGreeting(name).then(
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
<button type="submit">get a greeting</button>
|
|
339
|
-
</form>
|
|
340
|
-
)
|
|
341
|
-
}
|
|
342
|
-
```
|
|
343
|
-
|
|
344
|
-
> Note, in case it's not clear what's happening here, you could also write
|
|
345
|
-
> `handleSubmit` like this:
|
|
346
|
-
|
|
347
|
-
```javascript
|
|
348
|
-
function handleSubmit(event) {
|
|
349
|
-
event.preventDefault()
|
|
350
|
-
const name = event.target.elements.name.value
|
|
351
|
-
fetchGreeting(name).then(
|
|
352
|
-
newGreeting => setGreeting(newGreeting),
|
|
353
|
-
error => handleError(error),
|
|
354
|
-
)
|
|
113
|
+
response => {
|
|
114
|
+
// Set data in state and re-render
|
|
115
|
+
},
|
|
116
|
+
error => {
|
|
117
|
+
// Show error boundary
|
|
118
|
+
showErrorBoundary(error);
|
|
119
|
+
}
|
|
120
|
+
);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// Render ...
|
|
355
124
|
}
|
|
356
125
|
```
|
|
357
126
|
|
|
358
|
-
|
|
127
|
+
### `withErrorBoundary` HOC
|
|
128
|
+
This package can also be used as a [higher-order component](https://legacy.reactjs.org/docs/higher-order-components.html) that accepts all of the same props as above:
|
|
359
129
|
|
|
360
|
-
```
|
|
361
|
-
|
|
362
|
-
const [name, setName] = React.useState('')
|
|
363
|
-
const {greeting, error} = useGreeting(name)
|
|
364
|
-
useErrorHandler(error)
|
|
365
|
-
|
|
366
|
-
function handleSubmit(event) {
|
|
367
|
-
event.preventDefault()
|
|
368
|
-
const name = event.target.elements.name.value
|
|
369
|
-
setName(name)
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
return greeting ? (
|
|
373
|
-
<div>{greeting}</div>
|
|
374
|
-
) : (
|
|
375
|
-
<form onSubmit={handleSubmit}>
|
|
376
|
-
<label>Name</label>
|
|
377
|
-
<input id="name" />
|
|
378
|
-
<button type="submit">get a greeting</button>
|
|
379
|
-
</form>
|
|
380
|
-
)
|
|
381
|
-
}
|
|
382
|
-
```
|
|
383
|
-
|
|
384
|
-
In this case, if the `error` is ever set to a truthy value, then it will be
|
|
385
|
-
propagated to the nearest error boundary.
|
|
386
|
-
|
|
387
|
-
In either case, you could handle those errors like this:
|
|
388
|
-
|
|
389
|
-
```javascript
|
|
390
|
-
const ui = (
|
|
391
|
-
<ErrorBoundary FallbackComponent={ErrorFallback}>
|
|
392
|
-
<Greeting />
|
|
393
|
-
</ErrorBoundary>
|
|
394
|
-
)
|
|
395
|
-
```
|
|
396
|
-
|
|
397
|
-
And now that'll handle your runtime errors as well as the async errors in the
|
|
398
|
-
`fetchGreeting` or `useGreeting` code.
|
|
399
|
-
|
|
400
|
-
## Issues
|
|
401
|
-
|
|
402
|
-
_Looking to contribute? Look for the [Good First Issue][good-first-issue]
|
|
403
|
-
label._
|
|
404
|
-
|
|
405
|
-
### 🐛 Bugs
|
|
406
|
-
|
|
407
|
-
Please file an issue for bugs, missing documentation, or unexpected behavior.
|
|
408
|
-
|
|
409
|
-
[**See Bugs**][bugs]
|
|
410
|
-
|
|
411
|
-
### 💡 Feature Requests
|
|
412
|
-
|
|
413
|
-
Please file an issue to suggest new features. Vote on feature requests by adding
|
|
414
|
-
a 👍. This helps maintainers prioritize what to work on.
|
|
415
|
-
|
|
416
|
-
[**See Feature Requests**][requests]
|
|
417
|
-
|
|
418
|
-
## LICENSE
|
|
130
|
+
```js
|
|
131
|
+
import {withErrorBoundary} from 'react-error-boundary'
|
|
419
132
|
|
|
420
|
-
|
|
133
|
+
const ComponentWithErrorBoundary = withErrorBoundary(ExampleComponent, {
|
|
134
|
+
fallback: <div>Something went wrong</div>,
|
|
135
|
+
onError(error, info) {
|
|
136
|
+
// Do something with the error
|
|
137
|
+
// E.g. log to an error logging client here
|
|
138
|
+
},
|
|
139
|
+
})
|
|
421
140
|
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
[node]: https://nodejs.org
|
|
425
|
-
[build-badge]: https://img.shields.io/github/workflow/status/bvaughn/react-error-boundary/validate?logo=github&style=flat-square
|
|
426
|
-
[build]: https://github.com/bvaughn/react-error-boundary/actions?query=workflow%3Avalidate
|
|
427
|
-
[coverage-badge]: https://img.shields.io/codecov/c/github/bvaughn/react-error-boundary.svg?style=flat-square
|
|
428
|
-
[coverage]: https://codecov.io/github/bvaughn/react-error-boundary
|
|
429
|
-
[version-badge]: https://img.shields.io/npm/v/react-error-boundary.svg?style=flat-square
|
|
430
|
-
[package]: https://www.npmjs.com/package/react-error-boundary
|
|
431
|
-
[downloads-badge]: https://img.shields.io/npm/dm/react-error-boundary.svg?style=flat-square
|
|
432
|
-
[npmtrends]: https://www.npmtrends.com/react-error-boundary
|
|
433
|
-
[license-badge]: https://img.shields.io/npm/l/react-error-boundary.svg?style=flat-square
|
|
434
|
-
[license]: https://github.com/bvaughn/react-error-boundary/blob/master/LICENSE
|
|
435
|
-
[prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
|
|
436
|
-
[prs]: https://makeapullrequest.com
|
|
437
|
-
[coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square
|
|
438
|
-
[coc]: https://github.com/bvaughn/react-error-boundary/blob/master/CODE_OF_CONDUCT.md
|
|
439
|
-
[bugs]: https://github.com/bvaughn/react-error-boundary/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Acreated-desc+label%3Abug
|
|
440
|
-
[requests]: https://github.com/bvaughn/react-error-boundary/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3Aenhancement
|
|
441
|
-
[good-first-issue]: https://github.com/bvaughn/react-error-boundary/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3Aenhancement+label%3A%22good+first+issue%22
|
|
442
|
-
<!-- prettier-ignore-end -->
|
|
141
|
+
// Can be rendered as <ComponentWithErrorBoundary {...props} />
|
|
142
|
+
```
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Component, ComponentType, FunctionComponent, ReactElement, ReactNode, ErrorInfo, PropsWithChildren, PropsWithRef } from "react";
|
|
2
|
+
export type ErrorBoundaryContextType = {
|
|
3
|
+
didCatch: false;
|
|
4
|
+
error: any;
|
|
5
|
+
resetErrorBoundary: (...args: any[]) => void;
|
|
6
|
+
};
|
|
7
|
+
export const ErrorBoundaryContext: import("react").Context<ErrorBoundaryContextType | null>;
|
|
8
|
+
declare function FallbackRender(props: FallbackProps): ReactNode;
|
|
9
|
+
export type FallbackProps = {
|
|
10
|
+
error: any;
|
|
11
|
+
resetErrorBoundary: (...args: any[]) => void;
|
|
12
|
+
};
|
|
13
|
+
type ErrorBoundarySharedProps = {
|
|
14
|
+
onError?: (error: Error, info: {
|
|
15
|
+
componentStack: string;
|
|
16
|
+
}) => void;
|
|
17
|
+
onReset?: (details: {
|
|
18
|
+
reason: "imperative-api";
|
|
19
|
+
args: any[];
|
|
20
|
+
} | {
|
|
21
|
+
reason: "keys";
|
|
22
|
+
prev: any[] | undefined;
|
|
23
|
+
next: any[] | undefined;
|
|
24
|
+
}) => void;
|
|
25
|
+
resetKeys?: any[];
|
|
26
|
+
};
|
|
27
|
+
export type ErrorBoundaryPropsWithComponent = ErrorBoundarySharedProps & {
|
|
28
|
+
fallback?: never;
|
|
29
|
+
FallbackComponent: ComponentType<FallbackProps>;
|
|
30
|
+
fallbackRender?: never;
|
|
31
|
+
};
|
|
32
|
+
export type ErrorBoundaryPropsWithRender = ErrorBoundarySharedProps & {
|
|
33
|
+
fallback?: never;
|
|
34
|
+
FallbackComponent?: never;
|
|
35
|
+
fallbackRender: typeof FallbackRender;
|
|
36
|
+
};
|
|
37
|
+
export type ErrorBoundaryPropsWithFallback = ErrorBoundarySharedProps & {
|
|
38
|
+
fallback: ReactElement<unknown, string | FunctionComponent | typeof Component> | null;
|
|
39
|
+
FallbackComponent?: never;
|
|
40
|
+
fallbackRender?: never;
|
|
41
|
+
};
|
|
42
|
+
export type ErrorBoundaryProps = ErrorBoundaryPropsWithFallback | ErrorBoundaryPropsWithComponent | ErrorBoundaryPropsWithRender;
|
|
43
|
+
type ErrorBoundaryState = {
|
|
44
|
+
didCatch: boolean;
|
|
45
|
+
error: any;
|
|
46
|
+
};
|
|
47
|
+
export class ErrorBoundary extends Component<PropsWithRef<PropsWithChildren<ErrorBoundaryProps>>, ErrorBoundaryState> {
|
|
48
|
+
state: ErrorBoundaryState;
|
|
49
|
+
static getDerivedStateFromError(error: Error): {
|
|
50
|
+
didCatch: boolean;
|
|
51
|
+
error: Error;
|
|
52
|
+
};
|
|
53
|
+
resetErrorBoundary: (...args: any[]) => void;
|
|
54
|
+
componentDidCatch(error: Error, info: ErrorInfo): void;
|
|
55
|
+
componentDidUpdate(prevProps: ErrorBoundaryProps, prevState: ErrorBoundaryState): void;
|
|
56
|
+
render(): string | number | boolean | import("react").ReactElement<any, string | import("react").JSXElementConstructor<any>> | import("react").ReactFragment | import("react").FunctionComponentElement<import("react").ProviderProps<ErrorBoundaryContextType | null>> | null | undefined;
|
|
57
|
+
}
|
|
58
|
+
export type UseErrorBoundaryApi<Error> = {
|
|
59
|
+
resetBoundary: () => void;
|
|
60
|
+
showBoundary: (error: Error) => void;
|
|
61
|
+
};
|
|
62
|
+
export function useErrorBoundary<Error = any>(): UseErrorBoundaryApi<Error>;
|
|
63
|
+
export function withErrorBoundary<Props extends Object>(Component: ComponentType<Props>, errorBoundaryProps: ErrorBoundaryProps): ComponentType<Props>;
|
|
64
|
+
|
|
65
|
+
//# sourceMappingURL=react-error-boundary.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";AAEA,uCAAuC;IACrC,QAAQ,EAAE,KAAK,CAAC;IAChB,KAAK,EAAE,GAAG,CAAC;IACX,kBAAkB,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;CAC9C,CAAC;AAEF,OAAO,MAAM,8EACyC,CAAC;ACDvD,gCAAgC,KAAK,EAAE,aAAa,GAAG,SAAS,CAAC;AAEjE,4BAA4B;IAC1B,KAAK,EAAE,GAAG,CAAC;IACX,kBAAkB,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;CAC9C,CAAC;AAEF,gCAAgC;IAC9B,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE;QAAE,cAAc,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAC;IACnE,OAAO,CAAC,EAAE,CACR,OAAO,EACH;QAAE,MAAM,EAAE,gBAAgB,CAAC;QAAC,IAAI,EAAE,GAAG,EAAE,CAAA;KAAE,GACzC;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,GAAG,EAAE,GAAG,SAAS,CAAC;QAAC,IAAI,EAAE,GAAG,EAAE,GAAG,SAAS,CAAA;KAAE,KACrE,IAAI,CAAC;IACV,SAAS,CAAC,EAAE,GAAG,EAAE,CAAC;CACnB,CAAC;AAEF,8CAA8C,wBAAwB,GAAG;IACvE,QAAQ,CAAC,EAAE,KAAK,CAAC;IACjB,iBAAiB,EAAE,cAAc,aAAa,CAAC,CAAC;IAChD,cAAc,CAAC,EAAE,KAAK,CAAC;CACxB,CAAC;AAEF,2CAA2C,wBAAwB,GAAG;IACpE,QAAQ,CAAC,EAAE,KAAK,CAAC;IACjB,iBAAiB,CAAC,EAAE,KAAK,CAAC;IAC1B,cAAc,EAAE,qBAAqB,CAAC;CACvC,CAAC;AAEF,6CAA6C,wBAAwB,GAAG;IACtE,QAAQ,EAAE,aACR,OAAO,EACP,MAAM,GAAG,iBAAiB,GAAG,gBAAgB,CAC9C,GAAG,IAAI,CAAC;IACT,iBAAiB,CAAC,EAAE,KAAK,CAAC;IAC1B,cAAc,CAAC,EAAE,KAAK,CAAC;CACxB,CAAC;AAEF,iCACI,8BAA8B,GAC9B,+BAA+B,GAC/B,4BAA4B,CAAC;ACnCjC,0BAA0B;IAAE,QAAQ,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,GAAG,CAAA;CAAE,CAAC;AAO5D,0BAA2B,SAAQ,UACjC,aAAa,kBAAkB,kBAAkB,CAAC,CAAC,EACnD,kBAAkB,CACnB;IACC,KAAK,qBAAgB;IAErB,MAAM,CAAC,wBAAwB,CAAC,KAAK,EAAE,KAAK;;;;IAI5C,kBAAkB,YAAa,GAAG,EAAE,UAWlC;IAEF,iBAAiB,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,SAAS;IAI/C,kBAAkB,CAChB,SAAS,EAAE,kBAAkB,EAC7B,SAAS,EAAE,kBAAkB;IAyB/B,MAAM;CAoCP;AE3GD,gCAAgC,KAAK,IAAI;IACvC,aAAa,EAAE,MAAM,IAAI,CAAC;IAC1B,YAAY,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;CACtC,CAAC;AAEF,iCAAiC,KAAK,GAAG,GAAG,KAAK,oBAAoB,KAAK,CAAC,CAiC1E;ACtCD,kCAAkC,KAAK,SAAS,MAAM,EACpD,SAAS,EAAE,cAAc,KAAK,CAAC,EAC/B,kBAAkB,EAAE,kBAAkB,GACrC,cAAc,KAAK,CAAC,CActB","sources":["src/src/ErrorBoundaryContext.ts","src/src/types.ts","src/src/ErrorBoundary.ts","src/src/assertErrorBoundaryContext.ts","src/src/useErrorBoundary.ts","src/src/withErrorBoundary.ts","src/src/index.ts","src/index.ts"],"sourcesContent":[null,null,null,null,null,null,null,"export * from \"./ErrorBoundary\";\nexport * from \"./ErrorBoundaryContext\";\nexport * from \"./useErrorBoundary\";\nexport * from \"./withErrorBoundary\";\n\n// TypeScript types\nexport * from \"./types\";\n"],"names":[],"version":3,"file":"react-error-boundary.d.ts.map"}
|