I have this code to have a countdown on my site, going from 5 to 0 seconds:
const {useState, useCallback, useEffect} = React;
const Counter = ({isPlaying}) => {
const [seconds, setSeconds] = useState(5);
const [paused, setPaused] = useState(false);
const toggle = useCallback(() => {
setPaused(!paused);
}, [paused, setPaused]);
useEffect(() => {
const interval = setInterval(() => {
if (isPlaying && seconds > 0 && !paused) {
setSeconds(seconds => seconds - 1);
console.log('bla');
}
}, 1000);
return () => clearInterval(interval);
}, [seconds, isPlaying, paused]);
return (
<span className="count-down" id="count-down" onClick={toggle}>
<span>00</span>
<span>:</span>
<span>0<span id="sec-count">{seconds}</span></span>
</span>
);
}
ReactDOM.render(<Counter isPlaying={true}/>, document.body);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
in a functional component.
in Chrome and Firefox this works as expected. It counts down from 5 to 0, and you see the 'bla' in the console 5 times. But in Safari it doesn't re-render. It counts down only once or twice, so showing 3 or 4, after that it stops re-rendering. But the console still shows 'bla' 5 times.
Any idea why in Safari it stops re-rendering, even though the interval clearly still runs?