Skip to content

Commit cc27469

Browse files
committed
Added docs and plugged in onError() handler
1 parent e989407 commit cc27469

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

.flowconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[ignore]
2+
node_modules/.*
23

34
[include]
45

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# react-error-boundary
2+
3+
Sample reusable React error boundary component for React 16+
4+
5+
```jsx
6+
import ErrorBoundary from 'react-error-boundary';
7+
8+
// The simplest way to use a boundary;
9+
// Wrap around any component that may throw an error:
10+
<ErrorBoundary>
11+
<ComponentThatMayError />
12+
</ErrorBoundary>
13+
14+
// You can also react to errors (eg for logging):
15+
const myErrorHandler = (error, componentStack) => {/* ... */}
16+
<ErrorBoundary onError={myErrorHandler}>
17+
<ComponentThatMayError />
18+
</ErrorBoundary>
19+
20+
// You can also customize the fallback appearance:
21+
const MyFallbackComponent = ({ componentStack, error }) => (/* ... */)
22+
<ErrorBoundary FallbackComponent={MyFallbackComponent}>
23+
<ComponentThatMayError />
24+
</ErrorBoundary>
25+
```

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-error-boundary",
3-
"version": "0.0.5",
4-
"description": "Simple reusable React error boundary component",
3+
"version": "0.0.6",
4+
"description": "Sample reusable React error boundary component for React 16+",
55
"files": [
66
"dist"
77
],

src/ErrorBoundary.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import ErrorBoundaryFallbackComponent from './ErrorBoundaryFallbackComponent';
66
type Props = {
77
children?: any,
88
FallbackComponent: any,
9-
onError?: (error: Error) => void,
9+
onError?: (error: Error, componentStack: string) => void,
1010
};
1111

1212
type ErrorInfo = {
@@ -41,6 +41,14 @@ class ErrorBoundary extends Component {
4141
}
4242

4343
componentDidError(error: Error, info: ErrorInfo):void {
44+
const {onError} = this.props;
45+
46+
if (typeof onError === 'function') {
47+
try {
48+
onError(error, info ? info.componentStack : '');
49+
} catch (error) {}
50+
}
51+
4452
this.setState({error, info});
4553
}
4654

@@ -50,7 +58,7 @@ class ErrorBoundary extends Component {
5058

5159
if (error !== null) {
5260
return (
53-
<FallbackComponent componentStack={info && info.componentStack} error={error} />
61+
<FallbackComponent componentStack={info ? info.componentStack : ''} error={error} />
5462
);
5563
}
5664

src/ErrorBoundaryFallbackComponent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type Props = {
88
};
99

1010
const toTitle = (error: Error, componentStack: string): string => {
11-
return `${error}\n\nThis is located at:${componentStack}`;
11+
return `${error.toString()}\n\nThis is located at:${componentStack}`;
1212
};
1313

1414
const ErrorBoundaryFallbackComponent = ({ componentStack, error }: Props) => (

0 commit comments

Comments
 (0)