File tree Expand file tree Collapse file tree 5 files changed +39
-5
lines changed Expand file tree Collapse file tree 5 files changed +39
-5
lines changed Original file line number Diff line number Diff line change 1
1
[ignore]
2
+ node_modules/.*
2
3
3
4
[include]
4
5
Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change 1
1
{
2
2
"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+ " ,
5
5
"files" : [
6
6
" dist"
7
7
],
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ import ErrorBoundaryFallbackComponent from './ErrorBoundaryFallbackComponent';
6
6
type Props = {
7
7
children ?: any ,
8
8
FallbackComponent : any ,
9
- onError ?: ( error : Error ) => void ,
9
+ onError ?: ( error : Error , componentStack : string ) => void ,
10
10
} ;
11
11
12
12
type ErrorInfo = {
@@ -41,6 +41,14 @@ class ErrorBoundary extends Component {
41
41
}
42
42
43
43
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
+
44
52
this . setState ( { error, info} ) ;
45
53
}
46
54
@@ -50,7 +58,7 @@ class ErrorBoundary extends Component {
50
58
51
59
if ( error !== null ) {
52
60
return (
53
- < FallbackComponent componentStack = { info && info . componentStack } error = { error } />
61
+ < FallbackComponent componentStack = { info ? info . componentStack : '' } error = { error } />
54
62
) ;
55
63
}
56
64
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ type Props = {
8
8
} ;
9
9
10
10
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 } ` ;
12
12
} ;
13
13
14
14
const ErrorBoundaryFallbackComponent = ( { componentStack, error } : Props ) => (
You can’t perform that action at this time.
0 commit comments