-
Notifications
You must be signed in to change notification settings - Fork 0
/
useStateHook.html
50 lines (45 loc) · 1.45 KB
/
useStateHook.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Vanila-React</title>
<meta charset="UTF-8">
</head>
<body>
<div id="app"></div>
<script>
let state
// useState 내부에서 선언해 사용하면,
// render 함수가 실행이 될 떄마다 초기화 되기떄문에
// 함수 밖에서 선언이 되고 사용되게 된다.
const useState = (initState) => {
if(state === undefined) {
state = initState
}
const setState = (newState) => {
state = newState
render()
}
return [state, setState]
}
let lenderCount = 0
const render = () => {
app.innerHTML = `
<div>rederCount:${lenderCount}</div>
${Counter()}
`
lenderCount +=1
}
const Counter = () => {
const [count, setCount] = useState(1)
window.increment = () => setCount(count+1)
return `
<div>
<strong>count: ${count}</strong>
<button onclick="increment()">더하기</button>
</div>
`
}
render()
</script>
</body>
</html>