What is the purpose of the useEffect cleanup function?

WHAT IT TESTS: Your grasp of useEffect teardown before re-runs and unmount. ANSWER OUTLINE: Explain cleanup prevents memory leaks by clearing timers or unsubscribing when dependencies change or on unmount.
WHAT IT TESTS: Whether you understand useEffect cleanup as part of React's synchronization model, tearing down previous side effects before creating new ones. ANSWER OUTLINE: First, state cleanup runs before re-execution with changed dependencies and after unmount. Second, give concrete examples like clearing setInterval, aborting a fetch with AbortController, or unsubscribing from a store. Third, note that missing cleanup causes memory leaks and race conditions.
Read the original → react.dev
- #react
- #hooks
- #useeffect
- #cleanup
- #side-effects
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.