juniorReact Hooks
What is cleanup function in useEffect?
Updated May 6, 2026
Short answer
Cleanup function removes side effects like subscriptions or timers.
Deep explanation
Returned function in useEffect runs before unmount or before next effect execution.
Real-world example
Clearing intervals in timers or removing event listeners.
Common mistakes
- Not cleaning up causing memory leaks.
Follow-up questions
- When does cleanup run?
- Can cleanup be async?