This is a really simple question, I'm building a timer in React JS and I want to know if this is right.
There is a setInterval() on my app that every second decreases 1 to my "seconds" state, which is this one:
const [seconds, setSeconds] = useState(0)
const [minutes, setMinutes] = useState(0)
const [isPaused, setIsPaused] = useState(true)
timerId.current = setInterval(() => {
setSeconds(prev => prev - 1)
}, 1000);
And this useEffect checks EVERY SECOND if the timer is running AND if seconds is < than 0, to increase minutes and adds 59 more seconds.
useEffect(() => {
// if timer isn't running return
if (isPaused) {
return
}
// reset seconds and decrease minutes
if (seconds < 0) {
setSeconds(59)
setMinutes(prev => prev - 1)
}
}, [seconds])
My question here is, isn't it bad for the useEffect to run two if statements every second? Like, wouldn't it be bad for performance? And if it really is bad, what other options do I have?
Via Active questions tagged javascript - Stack Overflow https://ift.tt/zP4yRmk
Comments
Post a Comment