Skip to content
Guest
Learn React · Course Syllabus Show lessons
Hooks & Architecture · 9 min read · Intermediate

Side Effects & Cleanup (useEffect)

Synchronize components with external systems (fetch, timers, event listeners) using useEffect and cleanup functions.

What you'll learn

  • Understand when you need useEffect (external systems) vs. when you do not (derived data)
  • Declare accurate dependency arrays ([userId])
  • Return a cleanup function to cancel timers, listeners, or stale fetches

useEffect synchronizes a React component with an external system (DOM event listeners, timers, WebSockets, or data fetching). Always return a cleanup function whenever you subscribe to a listener or start an interval so React cleans it up on unmount.

Fill-in-the-Blank Exercise

Effect with Cleanup

Start an interval inside useEffect and clear it in the returned cleanup function.

(() => {
const id = setInterval(() => tick(), 1000);
return () => (id);
}, []);

Live Lifecycle Cleanup Demo

Effect Mount & Unmount Simulation
Live Result
Console Output

No console output yet. Try calling console.log(...) in JS.

Check Your Understanding

Score: 0 / 1

1. What does passing an empty dependency array [] to useEffect(() => { ... }, []) mean?

Study Workspace

Finished this lesson?

Your progress is saved locally in your browser, no account needed.

Search courses, lessons, and references

Type to search across all courses, lessons, and quick references.

Press Esc to close Powered by Pagefind