Exercise
State setter callback
Task
Fix the broken stopwatch
Right now the stopwatch goes from 0 to 1, but it gets stuck at 1. Fix the bug using the state setter callback.
Solution
React.useEffect(() => {
const intervalId = window.setInterval(() => {
setCount((count) => count + 1);
}, 1000);
return () => {
window.clearInterval(intervalId);
};
}, []);