#Why does this countdown start glitching out?
3 messages · Page 1 of 1 (latest)
when I go for this it just gets stuck on 119:
const Clock = () => {
const [time, setTime] = useState(120)
const interval = useRef();
useEffect(() => {
interval.current = setInterval(() => {
setTime(time - 1)
}, 1000)
return () => clearInterval(interval.current)
}, [])
if (time === 0) clearInterval(interval.current)
return (
<div>
{time}
</div>
)
};
oh adding time to dependencies seems to fix it