I am building a simple clock app with React. Currently the countDown()
function works, but I would like the user to be able to stop/start the clock by pressing a button. I have a state boolean called paused
that is inverted when the user clicks a button. The trouble is that after the value of paused
is inverted, the reference to paused
inside the countDown()
function passed to setInterval()
seems to be accessing the default value of paused
, instead of the updated value.
function Clock(){
const [sec, setSecs] = useState(sessionLength * 60);
const [paused, setPaused] = useState(false);
const playPause = () => {
setPaused(paused => !paused);
};
const countDown = () => {
if(!paused){
setSecs(sec => sec - 1)
}
}
useEffect(() => {
const interval = setInterval(() => {
countDown();
}, 1000);
return () => {
clearInterval(interval);
};
}, []);
I'm assuming it has something to do with the asynchronous nature of calls to setState()
in React, and/or the nature of scoping/context when using regular expressions. However I haven't been able to determine what is going on by reading documentation related to these concepts.
I can think of some workarounds that would allow my app to function as desired. However I want to understand what is wrong with my current approach. I would appreciate any light anyone can shed on this!
useCallback
? – WertuseEffect
is called only once when mounting the component. The countdown function registered inside will have its initial value at the time when theuseEffect/setInterval
is called. Sopaused
will only have the value when you initially mount the component. Because you are not callingcountDown
directly or updating its value inside your useEffect, it is not updated. – Sidewalksec
variable each time. What I don't understand is why the current value of the booleanpaused
isn't accessed. – MisvaluesetInterval
is called only one! You want only one timer at once to be running (in your use case at least). On the contrary the function you call inside, herecountDown
, is called every interval (1000ms in your exemple) – SidewalkcountDown
in the component body, every time your component re-renders, that function is re-defined. The one captured insetInterval()
is from the first render which also captures the firstpaused
state value. Basically a very nuanced example of the complexity involved in React's render cycle. This is a really good question to have on StackOverflow – Grandfather