Skip to content

Commit ce95324

Browse files
authored
Merge pull request #20 from rishabhsrao/improve-readme
Improve readme documentation
2 parents b3b6ab9 + 31774e8 commit ce95324

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

README.md

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
# react-error-boundary
1+
react-error-boundary
2+
====================
23

3-
Sample reusable React error boundary component for React 16+
4+
**A simple, reusable React error boundary component for React 16+.**
45

5-
The simplest way to use a boundary is to wrap it around any component that may throw an error.
6-
This will handle errors thrown by that component's descendants also.
6+
[![NPM registry](https://img.shields.io/npm/v/react-error-boundary.svg?style=for-the-badge)](https://yarnpkg.com/en/package/react-error-boundary)
7+
[![NPM license](https://img.shields.io/npm/l/react-error-boundary.svg?style=for-the-badge)](LICENSE)
8+
9+
React [v16](https://reactjs.org/blog/2017/09/26/react-v16.0.html) introduced the concept of [“error boundaries”](https://reactjs.org/docs/error-boundaries.html).
10+
11+
This component provides a simple and reusable wrapper that you can use to wrap around your components. Any rendering errors in your components hierarchy can then be gracefully handled.
12+
13+
# Usage
14+
15+
The simplest way to use `<ErrorBoundary>` is to wrap it around any component that may throw an error.
16+
This will handle errors thrown by that component and its descendants too.
717

818
```jsx
919
import ErrorBoundary from 'react-error-boundary';
@@ -13,40 +23,53 @@ import ErrorBoundary from 'react-error-boundary';
1323
</ErrorBoundary>
1424
```
1525

16-
You can react to errors (eg for logging) by providing an `onError` callback:
26+
You can react to errors (e.g. for logging) by providing an `onError` callback:
1727

1828
```jsx
1929
import ErrorBoundary from 'react-error-boundary';
2030

2131
const myErrorHandler = (error: Error, componentStack: string) => {
22-
// ...
32+
// Do something with the error
33+
// E.g. log to an error logging client here
2334
};
2435

2536
<ErrorBoundary onError={myErrorHandler}>
2637
<ComponentThatMayError />
2738
</ErrorBoundary>
2839
```
2940

30-
You can also customize the fallback appearance:
41+
You can also customize the fallback component’s appearance:
3142

3243
```jsx
44+
import { ErrorBoundary } from 'react-error-boundary';
45+
3346
const MyFallbackComponent = ({ componentStack, error }) => (
34-
<div/>
35-
)
47+
<div>
48+
<p><strong>Oops! An error occured!</strong></p>
49+
<p>Here’s what we know…</p>
50+
<p><strong>Error:</strong> {error.toString()}</p>
51+
<p><strong>Stacktrace:</strong> {componentStack}</p>
52+
</div>
53+
);
3654

3755
<ErrorBoundary FallbackComponent={MyFallbackComponent}>
3856
<ComponentThatMayError />
3957
</ErrorBoundary>
4058
```
4159

42-
You can also use it as a HOC:
60+
You can also use it as a [higher-order component](https://reactjs.org/docs/higher-order-components.html):
4361

4462
```jsx
45-
import {withErrorBoundary} from 'react-error-boundary';
63+
import { ErrorBoundaryFallbackComponent, withErrorBoundary } from 'react-error-boundary';
4664

4765
const ComponentWithErrorBoundary = withErrorBoundary(
48-
ComponentToDecorate: Element<any>,
49-
CustomFallbackComponent: ?Element<any>,
50-
onErrorHandler: ?(error: Error, componentStack: string) => void,
66+
ComponentThatMayError,
67+
ErrorBoundaryFallbackComponent, // Or pass in your own fallback component
68+
onErrorHandler: (error, componentStack) => {
69+
// Do something with the error
70+
// E.g. log to an error logging client here
71+
},
5172
);
52-
```
73+
74+
<ComponentWithErrorBoundary />
75+
```

0 commit comments

Comments
 (0)