Coding task · JavaScript and React course · Module 5: useEffect and Custom Hooks
Practice: Timer with useEffect
useEffect with setInterval: useEffect(() => { const id = setInterval(() => {...}, 1000); return () => clearInterval(id); }, []). Return function to cleanup! Empty array [] = run once on mount.
Task
Create [seconds, setSeconds] = useState(0). useEffect with setInterval every 1s: setSeconds(s => s + 1). IMPORTANT: return () => clearInterval(intervalId) - cleanup prevents memory leaks! Display: 'Mission time: {seconds}s'