Skip to content

Commit 19da714

Browse files
committed
effect needs latest state of timer (version 2)
1 parent 1982518 commit 19da714

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/useEffect-example/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ function App() {
99
let interval;
1010

1111
if (isOn) {
12-
interval = setInterval(() => setTimer(timer + 1), 1000);
12+
interval = setInterval(
13+
() => setTimer(timer => timer + 1),
14+
1000,
15+
);
1316
}
1417

1518
return () => clearInterval(interval);
1619
},
17-
[isOn, timer],
20+
[isOn],
1821
);
1922

2023
const onReset = () => {

0 commit comments

Comments
 (0)