Not all side effects are "fire-and-forget". Many side effects establish connections, timers, or event listeners that will cause serious memory leaks, duplicated callbacks, or state updates on unmounted components if not properly disassembled when the component disappears:
setInterval, setTimeoutwindow.addEventListener('resize', ...)AbortControllerTo clean up after side effects, return a cleanup function from your useEffect callback.
setIntervalIf you omit clearInterval(timerId), the timer will keep running in the background forever—even after the user navigates to a completely different page—leaking memory and triggering phantom state updates.
AbortControllerIf a user rapidly toggles between different tabs or pages, an earlier API request might finish after the component has already unmounted. Use standard browser AbortController to cancel in-flight requests:
useEffect?setInterval timer is created in useEffect without a cleanup function?fetch requests inside a useEffect cleanup function?<React.StrictMode> run effect setup and cleanup twice during development?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| The Dependency Array and Execution Lifecycle | Inline Styles and Style Objects |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.