-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.html
101 lines (79 loc) · 3.31 KB
/
counter.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="root"></div>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script type="importmap">
{
"imports": {
"react": "https://esm.sh/react?dev",
"react-dom/client": "https://esm.sh/react-dom/client?dev"
}
}
</script>
<script type="text/babel" data-type="module">
// import React, { StrictMode, Component } from 'react'
// import { createRoot } from 'react-dom/client'
// class App extends Component {
// constructor() {
// super()
// this.state = { count: 0 }
// }
// componentDidMount() {
// setInterval(() => this.setState({ count: this.state.count + 1 }), 1000)
// }
// render() {
// return <h1>{this.state.count}</h1>
// }
// }
// const root = createRoot(document.getElementById('root'))
// root.render(<StrictMode>
// <App />
// </StrictMode>)
// import React, { StrictMode, useState, useEffect } from 'react'
// import { createRoot } from 'react-dom/client'
// // it doesn't work... why? (solution on next file, but think first)
// function App() {
// const [count, setCount] = useState(0)
// useEffect(() => {
// setInterval(() => setCount(count + 1), 1000)
// /*
// the answer is here, in the previous line
// a new state is set adding 1 to the previous state (count)
// the closure (interval callback) is initialized once at the beginning (effect)
// and as a closure, it has "photographic memory" of the value of outer scope variables
// that is, the count variable
// it memorizes that variable value when the App function was called the first time (first render)
// the use effect do never creates a new interval again (that's the purpose, not to do that in this case, only once at the beginning)
// so this closure (interval callback) will never have the chance to see a new value for outer variable count
// that's why it always sets the same count (value 1) to the state
// */
// }, [])
// return <h1>{count}</h1>
// }
// const root = createRoot(document.getElementById('root'))
// root.render(<StrictMode>
// <App />
// </StrictMode>)
import React, { StrictMode, useState, useEffect } from 'react'
import { createRoot } from 'react-dom/client'
function App() {
const [count, setCount] = useState(0)
useEffect(() => {
setTimeout(() => setCount(count + 1), 1000)
}, [count])
return <h1>{count}</h1>
}
const root = createRoot(document.getElementById('root'))
root.render(<StrictMode>
<App />
</StrictMode>)
</script>
</body>
</html>