File tree Expand file tree Collapse file tree 2 files changed +14
-10
lines changed Expand file tree Collapse file tree 2 files changed +14
-10
lines changed Original file line number Diff line number Diff line change @@ -2,9 +2,9 @@ import React from 'react';
2
2
import ReactDOM from 'react-dom' ;
3
3
4
4
// import App from './useState-example';
5
- // import App from './useEffect-example';
5
+ import App from './useEffect-example' ;
6
6
// import App from './customHook-example';
7
- import App from './useDataApiHook-example' ;
7
+ // import App from './useDataApiHook-example';
8
8
9
9
import * as serviceWorker from './serviceWorker' ;
10
10
Original file line number Diff line number Diff line change @@ -4,14 +4,18 @@ function App() {
4
4
const [ isOn , setIsOn ] = useState ( false ) ;
5
5
const [ timer , setTimer ] = useState ( 0 ) ;
6
6
7
- useEffect ( ( ) => {
8
- const interval = setInterval (
9
- ( ) => isOn && setTimer ( timer + 1 ) ,
10
- 1000 ,
11
- ) ;
12
-
13
- return ( ) => clearInterval ( interval ) ;
14
- } , [ ] ) ;
7
+ useEffect (
8
+ ( ) => {
9
+ let interval ;
10
+
11
+ if ( isOn ) {
12
+ interval = setInterval ( ( ) => setTimer ( timer + 1 ) , 1000 ) ;
13
+ }
14
+
15
+ return ( ) => clearInterval ( interval ) ;
16
+ } ,
17
+ [ isOn ] ,
18
+ ) ;
15
19
16
20
const onReset = ( ) => {
17
21
setIsOn ( false ) ;
You can’t perform that action at this time.
0 commit comments