-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex4_state.html
88 lines (72 loc) · 1.96 KB
/
ex4_state.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello React</title>
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<!-- compilation JSX => JS -->
<script type="text/babel">
class App extends React.Component{
constructor(props) {
super(props);
this.state = {
unmount : false
}
this.cmpt = 0
}
render(){
return (!this.state.unmount && <Clock/>);
}
componentDidMount(){
setTimeout(() => {
this.setState({
unmount: true
})
},5000)
}
}
class Clock extends React.Component{
constructor(props){
super(props);
this.state = {
time: new Date().toLocaleTimeString()
}
this.timer = null
}
componentDidMount(){
console.log('Component is mounted');
this.timer = setInterval(() => {
this.setState({
time: new Date().toLocaleTimeString()
})
}, 1000);
}
componentDidUpdate(){
console.log('Render is updated');
}
componentWillUnmount(){
console.log('Component is unmounted');
clearInterval(this.timer)
}
render() {
return (
<div className="horloge">
{this.state.time}
</div>
)
}
}
// TODO
const container = document.getElementById('root');
ReactDOM.render(
<App />,
container
);
</script>
</body>
</html>