File tree Expand file tree Collapse file tree 1 file changed +8
-11
lines changed Expand file tree Collapse file tree 1 file changed +8
-11
lines changed Original file line number Diff line number Diff line change 1
- import { useEffect , useState } from 'react' ;
2
-
1
+ import { useCounter } from '../useCounter/useCounter' ;
3
2
import { useLatest } from './useLatest' ;
4
3
5
4
const Demo = ( ) => {
6
- const [ count , setCount ] = useState ( 0 ) ;
7
- const latestCount = useLatest ( count ) ;
8
-
9
- useEffect ( ( ) => {
10
- const interval = setInterval ( ( ) => setCount ( count + 1 ) , 1000 ) ;
11
- return ( ) => clearInterval ( interval ) ;
12
- } , [ ] ) ;
5
+ const counter = useCounter ( ) ;
6
+ const latestCount = useLatest ( counter . value ) ;
13
7
14
8
return (
15
9
< div >
16
- < p > Count: < code > { count } </ code > </ p >
17
- < p > Latest count: < code > { latestCount } </ code > </ p >
10
+ < p > You clicked < code > { counter . value } </ code > times, latest count is < code > { latestCount } </ code > </ p >
11
+
12
+ < button className = 'button' type = 'button' onClick = { ( ) => counter . inc ( ) } >
13
+ Increment
14
+ </ button >
18
15
</ div >
19
16
) ;
20
17
} ;
You can’t perform that action at this time.
0 commit comments