-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathii_debug.js
More file actions
67 lines (58 loc) · 1.44 KB
/
ii_debug.js
File metadata and controls
67 lines (58 loc) · 1.44 KB
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
65
66
67
import React from 'react'
import ReactDOM from 'react-dom'
function replacer(key, value) {
if (typeof value === 'function') {
return `function ${value.name}() {...}`
}
return value
}
export function stringify(value) {
return JSON.stringify(value, replacer, 2)
}
// II debug example
// We are using the Inheritance Inversion technique to display
// the current state and props of the WrappedComponent (the component you want to debug).
// This is based on the technique that Mickael Jackson and Ryan Florence recommend
export function IIHOC(WrappedComponent) {
return class II extends WrappedComponent {
render() {
return (
<div>
<h2>
HOC Debugger Component
</h2>
<p>
Props
</p>
<pre>{stringify(this.props)}</pre>
<p>
State
</p>
<pre>{stringify(this.state)}</pre>
{super.render()}
</div>
)
}
}
}
class Example extends React.Component {
constructor(props) {
super(props)
this.state = {
name: 'fran',
email: 'franleplant@gmail.com'
}
}
render() {
return (
<div>
<h2>
Wrapped Component
</h2>
<p>Im a wrapped component</p>
</div>
)
}
}
const EnhancedExample = IIHOC(Example)
ReactDOM.render(<EnhancedExample date={(new Date).toISOString()} callback={function test() {}}/>, document.getElementById('root'))