Effect Cleanup
Return a function from an effect to clean up timers and listeners. React runs it before re-running the effect and on unmount.
Example
useEffect(() => {
const id = setInterval(() => setT((t) => t + 1), 1000);
return () => clearInterval(id);
}, []);Exercise
Cleanup returns a ___.
return () => clearInterval(id) is a Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.