Skip to content
Learn Netverks

Lesson

Step 18/36 50% through track

useeffect-basics

useEffect basics

Last reviewed May 28, 2026 Content v20260528
Track mode
client_react
Means
In-browser React TSX
Reading
~2 min
Level
intermediate

This lesson

This lesson teaches useEffect basics: the concepts, APIs, and habits you need before advancing in React.

Side effects (fetch, subscriptions, timers) must be modeled explicitly—this lesson prevents infinite loops and stale closures.

You will apply useEffect basics in contexts like: SPAs, dashboards, design-system-driven products, and React Native mobile apps.

Write TypeScript/TSX, click Run in browser—React 18 loads from CDN, JSX compiles in the tab, UI renders in the preview root, and printOutput feeds the terminal.

When you can explain the previous lesson's ideas without copying starter code.

Effects run after render when you need to synchronize with something outside React: timers, subscriptions, manual DOM APIs, or simulated fetches. useEffect is the primary hook for this.

Basic shape

const { useEffect, useState } = React;

useEffect(() => {
  // side effect
  return () => {
    // optional cleanup
  };
}, [dependencies]);

When effects run

  • After paint, asynchronously—does not block the browser from showing UI
  • Re-runs when dependencies in the array change
  • Empty array [] — run once after mount (plus cleanup on unmount)
  • Omitting the array — runs after every render (rare; easy to cause loops)

Important interview questions and answers

  1. Q: useEffect vs event handler?
    A: Handlers respond to user actions; effects respond to render/state/prop changes and external sync.
  2. Q: Can you set state in useEffect?
    A: Yes, but guard against infinite loops—only set state when something actually changed.

Self-check

  1. What does an empty dependency array mean?
  2. When should you return a cleanup function?

Challenge

Document title

  1. Type in the input and watch document.title update (check the browser tab in the iframe).
  2. Confirm cleanup resets the title on unmount by clearing the preview.

Done when: document.title matches the input while the component is mounted.

Pitfall: An effect with no dependency array runs after every render—almost always a bug for fetch/subscribe logic.

Interview prep

When should you use useEffect?

To synchronize with external systems—fetch, subscriptions, timers—not to derive display data from props. The dependency array controls re-runs; cleanup runs before re-run and unmount.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs in your browser in a sandboxed frame. Backend runners appear when this track’s profile allows them.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Effect you wrote—deps?
  • What not to put in effect?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump