File tree Expand file tree Collapse file tree 2 files changed +47
-0
lines changed
Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ import React , { useState , useMemo , useCallback } from "react" ;
2+
3+ const words = [ "hey" , "this" , "is" , "cool" ] ;
4+
5+ function App ( ) {
6+ const [ count , setCount ] = useState ( 0 ) ;
7+ const [ wordIndex , setWordIndex ] = useState ( 0 ) ;
8+
9+ const word = words [ wordIndex ] ;
10+
11+ const computeLetterCount = useCallback ( word => {
12+ let i = 0 ;
13+ while ( i < 1000000000 ) i ++ ; // Fake expense
14+ return word . length ;
15+ } , [ ] ) ;
16+
17+ const letterCount = useMemo ( ( ) => {
18+ console . log ( "Calculate letter count..." ) ;
19+ return computeLetterCount ( word ) ;
20+ } , [ word , computeLetterCount ] ) ;
21+
22+ return (
23+ < >
24+ < h2 > Compute number of letters</ h2 >
25+ < p >
26+ "{ word } " has { letterCount } letters
27+ </ p >
28+ < button
29+ onClick = { ( ) => {
30+ const nextWordIndex =
31+ wordIndex + 1 === words . length ? 0 : wordIndex + 1 ;
32+ setWordIndex ( nextWordIndex ) ;
33+ } }
34+ >
35+ Next word
36+ </ button >
37+ < h2 > Increment a counter</ h2 >
38+ < p > Counter: { count } </ p >
39+ < button onClick = { ( ) => setCount ( count + 1 ) } > Increment</ button >
40+ </ >
41+ ) ;
42+ }
43+
44+ export default App ;
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ import ContextClass from './Context/Class';
1717import ContextFunc from './Context/Function' ;
1818import LifecycleClass from './Lifecycle/Class' ;
1919import LifecycleFunc from './Lifecycle/Function' ;
20+ import Memo from './Memo/Function' ;
2021import CustomHook from './CustomHooks/Function' ;
2122
2223
@@ -33,6 +34,7 @@ ReactDOM.render(
3334 < li > < Link to = "/contextFunc" > Context Func</ Link > </ li >
3435 < li > < Link to = "/lifecycleClass" > Lifecycle Class</ Link > </ li >
3536 < li > < Link to = "/lifecycleFunc" > Lifecycle Func</ Link > </ li >
37+ < li > < Link to = "/memo" > Memo</ Link > </ li >
3638 < li > < Link to = "/customHook" > Custom Hook</ Link > </ li >
3739 </ ul >
3840 </ nav >
@@ -45,6 +47,7 @@ ReactDOM.render(
4547 < Route exact path = "/contextFunc" component = { ContextFunc } />
4648 < Route exact path = "/lifecycleClass" component = { ( ) => < LifecycleClass name = "John" /> } />
4749 < Route exact path = "/lifecycleFunc" component = { ( ) => < LifecycleFunc name = "John" /> } />
50+ < Route exact path = "/memo" component = { Memo } />
4851 < Route exact path = "/customHook" component = { ( ) => < CustomHook name = "John" /> } />
4952 </ Switch >
5053 </ NameContext . Provider >
You can’t perform that action at this time.
0 commit comments