forked from kentcdodds/beginners-guide-to-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
18-hook-flow.html
124 lines (107 loc) · 3.34 KB
/
18-hook-flow.html
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<body>
<div id="root"></div>
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/[email protected]/babel.js"></script>
<script type="text/babel">
// https://github.com/donavon/hook-flow
function Child() {
console.log('%c Child: render start', 'color: MediumSpringGreen')
const [count, setCount] = React.useState(() => {
console.log('%c Child: useState callback', 'color: tomato')
return 0
})
React.useEffect(() => {
console.log('%c Child: useEffect no deps', 'color: LightCoral')
return () => {
console.log(
'%c Child: useEffect no deps cleanup',
'color: LightCoral',
)
}
})
React.useEffect(() => {
console.log(
'%c Child: useEffect empty deps',
'color: MediumTurquoise',
)
return () => {
console.log(
'%c Child: useEffect empty deps cleanup',
'color: MediumTurquoise',
)
}
}, [])
React.useEffect(() => {
console.log('%c Child: useEffect with dep', 'color: HotPink')
return () => {
console.log(
'%c Child: useEffect with dep cleanup',
'color: HotPink',
)
}
}, [count])
const element = (
<button onClick={() => setCount(previousCount => previousCount + 1)}>
{count}
</button>
)
console.log('%c Child: render end', 'color: MediumSpringGreen')
return element
}
function App() {
console.log('%cApp: render start', 'color: MediumSpringGreen')
const [showChild, setShowChild] = React.useState(() => {
console.log('%cApp: useState callback', 'color: tomato')
return false
})
React.useEffect(() => {
console.log('%cApp: useEffect no deps', 'color: LightCoral')
return () => {
console.log('%cApp: useEffect no deps cleanup', 'color: LightCoral')
}
})
React.useEffect(() => {
console.log('%cApp: useEffect empty deps', 'color: MediumTurquoise')
return () => {
console.log(
'%cApp: useEffect empty deps cleanup',
'color: MediumTurquoise',
)
}
}, [])
React.useEffect(() => {
console.log('%cApp: useEffect with dep', 'color: HotPink')
return () => {
console.log('%cApp: useEffect with dep cleanup', 'color: HotPink')
}
}, [showChild])
const element = (
<>
<label>
<input
type="checkbox"
checked={showChild}
onChange={e => setShowChild(e.target.checked)}
/>{' '}
show child
</label>
<div
style={{
padding: 10,
margin: 10,
height: 30,
width: 30,
border: 'solid',
}}
>
{showChild ? <Child /> : null}
</div>
</>
)
console.log('%cApp: render end', 'color: MediumSpringGreen')
return element
}
ReactDOM.render(<App />, document.getElementById('root'))
</script>
</body>