File tree Expand file tree Collapse file tree 3 files changed +41
-0
lines changed Expand file tree Collapse file tree 3 files changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ import React from 'react' ;
3
+
4
+ function MyComponent ( { name} ) {
5
+ /* 使用 props 渲染 */
6
+ console . log ( '子组件渲染了' ) ;
7
+ return (
8
+ < div >
9
+ { name }
10
+ </ div >
11
+ )
12
+ }
13
+ function areEqual ( prevProps , nextProps ) {
14
+ //true则不重新渲染, 相反则重新渲染
15
+ return true ;
16
+ }
17
+ export default React . memo ( MyComponent , areEqual ) ;
Original file line number Diff line number Diff line change
1
+
2
+ import React , { useState } from 'react' ;
3
+ import Memo from '../memo/index'
4
+
5
+ function Page ( ) {
6
+ const [ name , setName ] = useState ( 'Ming' ) ;
7
+ const count = 10 ;
8
+ return (
9
+ < div style = { { marginLeft : 50 } } >
10
+ < h2 > 子组件在任何情况下都不重新渲染</ h2 >
11
+ < p > { name } </ p >
12
+ < button onClick = { ( ) => setName ( 'Ming' ) } > Ming</ button > < br /> < br />
13
+ < button onClick = { ( ) => setName ( 'Anny' ) } > Anny</ button > < br /> < br />
14
+ < Memo name = { count } />
15
+ </ div >
16
+ )
17
+ }
18
+
19
+ export default Page ;
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import EchartBox from '../page/echartBox/index'
12
12
import ChinaMap from '../page/chinaMap/index'
13
13
import UseMemo from '../page/useMemo/index'
14
14
import UseCallback from '../page/useCallback/index'
15
+ import Memo from '../page/memoParent/index'
15
16
16
17
function Header ( ) {
17
18
return (
@@ -45,6 +46,9 @@ function Header() {
45
46
< li >
46
47
< Link to = "/optimize" > 性能优化之shouldComponentUpdate,及第三方动画库做简单动画</ Link >
47
48
</ li >
49
+ < li >
50
+ < Link to = "/memo" > 性能优化之memo,相当于函数组件里的shouldComponentUpdate</ Link >
51
+ </ li >
48
52
< li >
49
53
< Link to = "/echart" > echart的简单应用</ Link >
50
54
</ li >
@@ -81,6 +85,7 @@ function App() {
81
85
< Route path = "/map" component = { ChinaMap } />
82
86
< Route path = "/useMemo" component = { UseMemo } />
83
87
< Route path = "/useCallback" component = { UseCallback } />
88
+ < Route path = "/memo" component = { Memo } />
84
89
</ Switch >
85
90
</ div >
86
91
</ BrowserRouter >
You can’t perform that action at this time.
0 commit comments