-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-3.js
More file actions
61 lines (52 loc) · 1.26 KB
/
example-3.js
File metadata and controls
61 lines (52 loc) · 1.26 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
// 案例 3
const MyReact = (function () {
let _val, _deps // 在模块作用域内保留状态、依赖列表(useEffect 使用)
return {
render(Component) {
const Comp = Component()
Comp.render()
return Comp
},
useEffect(callback, depArray) {
const hasNoDeps = !depArray
const hasChangedDeps = _deps
? /* 非首次调用 */ depArray.some((dep, i) => !Object.is(dep, _deps[i]))
: /* 首次调用 */true
if (hasNoDeps || hasChangedDeps) {
callback()
_deps = depArray
}
},
useState(initialValue) {
// 每次调用时,增加 _val 值赋值判断
_val = _val || initialValue
function setState(newVal) {
_val = newVal
}
return [_val, setState]
}
}
})()
// 使用
function Counter() {
const [count, setCount] = MyReact.useState(0) // 使用之前实现的 useState()
MyReact.useEffect(() => {
console.log('[effect]', count)
}, [count])
return {
click() {
setCount(count + 1)
},
render() {
console.log('[render]', { count: count })
}
}
}
let App
App = MyReact.render(Counter)
// [effect] 0
// [render] { count: 0 }
App.click()
App = MyReact.render(Counter)
// [effect] 1
// [render] { count: 1 }