forked from kossnocorp/react-guard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaive.js
61 lines (59 loc) · 1.75 KB
/
naive.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
// XXX: This is a naive implementation, see index.js
// for optimized, production verion.
var naiveReactGuard = function (React, guardFn) {
guardFn =
guardFn ||
function () {
return null
}
React.__reactGuardOriginalCreateElement__ = React.createElement
React.createElement = function (type) {
if (
typeof type === 'function' &&
type.prototype &&
'render' in type.prototype &&
!('__guardedRender__' in type.prototype)
) {
type.prototype.__guardedRender__ = type.prototype.render
type.prototype.render = function () {
try {
return this.__guardedRender__()
} catch (err) {
return guardFn(err, {
props: this.props,
state: this.state,
displayName: this.constructor.displayName || this.constructor.name
})
}
}
} else if (
typeof type === 'function' &&
(!type.prototype || !('render' in type.prototype))
) {
var guardedType = type
var _type
if (guardedType.__reactGuardGuardedFunction__) {
_type = guardedType.__reactGuardGuardedFunction__
} else {
_type = function (props, publicContext, updateQueue) {
try {
return guardedType(props, publicContext, updateQueue)
} catch (err) {
return guardFn(err, {
props: props,
displayName: guardedType.displayName
})
}
}
Object.assign(_type, guardedType)
guardedType.__reactGuardGuardedFunction__ = _type
}
type = _type
}
return React.__reactGuardOriginalCreateElement__.apply(
React,
[type].concat(Array.prototype.slice.call(arguments, 1))
)
}
}
module.exports = naiveReactGuard