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!