You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Readme.md
+26-13Lines changed: 26 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,12 +7,17 @@ Now you can more easily diagnose errors during development.
7
7
8
8
NOTE: It's not likely a good idea to run with this `enabled` in production as it could affect performance of your React components.
9
9
10
+
### More details
11
+
12
+
You can follow [this GitHub Issue](https://github.com/facebook/react/issues/2461) for more details.
13
+
Thanks to inspiration from [skiano/react-safe-render](https://github.com/skiano/react-safe-render/blob/feature/safe-methods/index.js).
14
+
10
15
# Usage with an es7 @decorator
11
16
```
12
17
import wrapReactLifecycleMethodsWithTryCatch from 'react-component-errors'
13
18
14
19
@wrapReactLifecycleMethodsWithTryCatch
15
-
class MyComponent {
20
+
class MyComponent extends React.Component {
16
21
componentDidMount(){
17
22
throw new Error("Test error");
18
23
}
@@ -27,7 +32,7 @@ class MyComponent {
27
32
```
28
33
import wrapReactLifecycleMethodsWithTryCatch from 'react-component-errors'
29
34
30
-
class MyComponent {
35
+
class MyComponent extends React.Component {
31
36
componentDidMount(){
32
37
throw new Error("Test error");
33
38
}
@@ -48,26 +53,34 @@ import {config} from 'react-component-errors'
48
53
`config.errorHandler`: default will `console.error` a helpful error message. See example below to override and customize errorHandler.
49
54
50
55
# Override errorHandler using config
56
+
57
+
You can see the below running [in a plnkr](http://plnkr.co/edit/VlYsps?p=preview) where we give the helper our own `errorHandler` which uses [Toastr](http://codeseven.github.io/toastr/) to display error messages.
58
+
51
59
```
52
-
import wrapReactLifecycleMethodsWithTryCatch, {config} from 'react-component-errors'
60
+
'use strict';
53
61
54
-
config.errorHandler = (errorReport) => {
55
-
// Do something custom with errorReport
62
+
import React from 'react';
63
+
import ReactDOM from 'react-dom';
64
+
import toastr from 'toastr';
65
+
import wrapReactLifecycleMethodsWithTryCatch, {config} from 'npm:react-component-errors';
56
66
57
-
console.error(errorReport.error) // the Error caught
58
-
console.error(errorReport.component) // the component's name (constructor function)
59
-
console.error(errorReport.method) // the method that we caught an error in (EX: `render`)
60
-
console.error(errorReport.props) // the props passed to the component
61
-
console.error(errorReport.arguments) // any arguments passed to the lifecycle method (if there were any)
67
+
config.errorHandler = (errorReport) => {
68
+
console.error(`Error in ${errorReport.component}.${errorReport.method}(${(errorReport.arguments ? '...' : '')}): ${errorReport.error}`, errorReport);
69
+
toastr.error(`Error in ${errorReport.component}.${errorReport.method}(${(errorReport.arguments ? '...' : '')}): ${errorReport.error}`);
0 commit comments