React Bug Hunt Medium +20 XP
useEffect Cleanup
This component leaks a timer when unmounted.
ReactFind the bug
1
useEffect(() => {2
const id = setInterval(() => tick(), 1000);3
}, []); The bug is on line 3
Select the correct fix
Show answer and explanation
Answer: Return a cleanup function that calls clearInterval(id)
useEffect can return a cleanup function that runs when the component unmounts. Use it to clear timers and subscriptions.
Hint: useEffect cleanup prevents resource leaks on unmount.
What this challenge tests
Bug Hunt in React: useEffect, cleanup, memory-leak.