Skip to content

Commit 9281f6c

Browse files
committed
updating docs
1 parent 74f86f4 commit 9281f6c

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

Readme.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ Now you can more easily diagnose errors during development.
77

88
NOTE: It's not likely a good idea to run with this `enabled` in production as it could affect performance of your React components.
99

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+
1015
# Usage with an es7 @decorator
1116
```
1217
import wrapReactLifecycleMethodsWithTryCatch from 'react-component-errors'
1318
1419
@wrapReactLifecycleMethodsWithTryCatch
15-
class MyComponent {
20+
class MyComponent extends React.Component {
1621
componentDidMount(){
1722
throw new Error("Test error");
1823
}
@@ -27,7 +32,7 @@ class MyComponent {
2732
```
2833
import wrapReactLifecycleMethodsWithTryCatch from 'react-component-errors'
2934
30-
class MyComponent {
35+
class MyComponent extends React.Component {
3136
componentDidMount(){
3237
throw new Error("Test error");
3338
}
@@ -48,26 +53,34 @@ import {config} from 'react-component-errors'
4853
`config.errorHandler`: default will `console.error` a helpful error message. See example below to override and customize errorHandler.
4954

5055
# 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+
5159
```
52-
import wrapReactLifecycleMethodsWithTryCatch, {config} from 'react-component-errors'
60+
'use strict';
5361
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';
5666
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}`);
6270
};
6371
6472
@wrapReactLifecycleMethodsWithTryCatch
65-
class MyComponent {
66-
componentDidMount(){
73+
class MyComponent extends React.Component {
74+
75+
componentWillMount(){
6776
throw new Error("Test error");
6877
}
78+
6979
render(){
70-
return <div>Hello</div>;
80+
return <div>{this.state.message}</div>;
7181
}
7282
}
83+
84+
85+
ReactDOM.render(<MyComponent />, document.getElementById('main'));
7386
```

0 commit comments

Comments
 (0)