-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
64 lines (50 loc) · 1.62 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// TODO: protect errors in error handler?
module.exports = function safeRender (React, config) {
config = config || {};
config.errorHandler = config.errorHandler || function () {};
if (React.hasOwnProperty('unsafeCreateClass')) { return; }
React.unsafeCreateClass = React.createClass;
React.createClass = function (spec) {
var componentClass = {};
function wrap(method, returnFn) {
if (!spec.hasOwnProperty(method)) { return; }
var unsafe = spec[method];
spec[method] = function () {
try {
return unsafe.apply(this, arguments);
} catch (e) {
var report = {
displayName: componentClass.displayName,
method: method,
props: this.props,
error: e
};
if (arguments.length > 0) {
report.arguments = arguments;
}
config.errorHandler(report);
return typeof returnFn === 'function' ? returnFn.apply(this, arguments) : null;
}
};
}
wrap('render');
wrap('componentWillMount');
wrap('componentDidMount');
wrap('componentWillReceiveProps');
wrap('shouldComponentUpdate', safeShouldComponentUpdate);
wrap('componentWillUpdate');
wrap('componentDidUpdate');
wrap('componentWillUnmount');
wrap('getInitialState', safeGetInitial);
// store ref to class in closure so error reporting can be more specific
componentClass = React.unsafeCreateClass.apply(React, arguments);
return componentClass;
};
return React;
};
function safeShouldComponentUpdate() {
return true;
}
function safeGetInitial() {
return {};
}