I have a timer using setInterval() in a React component and I\'m unsure what the best practices are in order to start and stop this interval in respect to using
setInterval()
Using React Hooks useState and useEffect you can do the following:
useState
useEffect
const [timer, setTimer] = useState(1); useEffect(() => { const timerId = setInterval(() => setTimer(timer + 1), 1000); return () => clearInterval(timerId); });