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
|
import React, { useEffect, useState } from "react";
import { HashRouter as Router,Route ,Switch} from "react-router-dom";
import { Link } from "react-router-dom";
function Child1() {
return (
<div>
<h2>
hello world child1
</h2>
</div>
)
}
function Child2() {
return (
<div>
<h2>
child2
</h2>
</div>
)
}
export default function LifeCycleExample() {
const [cnt , setCnt] = useState(0)
useEffect(()=>{
//useEffect 不影响视图更新,异步延迟执行,不是同步的
console.log(`component did update, UI 更新了 , cnt := ${cnt}`)
})
return (
<div>
<Router >
<button onClick={()=>setCnt(cnt+1)}>
点击 {cnt}
</button>
11
<br />
<Link to="./" >child1</Link>
<br />
<Link to="child2" >child2</Link>
<Switch>
<Route path="/LifeCycle/child2" exact component={Child2} />
<Route path="/LifeCycle/child1" exact component={Child1} />
<Route path="/LifeCycle/" exact component={Child1} />
</Switch>
</Router>
</div>
)
}
|