Lesson 24/100

Tutorials React.js Tutorial

useEffect — Complete Guide

useEffect — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of React.js Tutorial on Toolliyo Academy.

On this page

React.js Tutorial · Lesson 24 of 100

useEffect

BeginnerIntermediateAdvancedProfessional

Beginner · 1 — Learn by example · ~6 min · Module 3: Forms & Hooks

What is this?

useEffect runs side effects after render — fetching data, subscribing to events, or syncing with document title. You can return a cleanup function.

Why should you care?

Data fetching and subscriptions do not belong inside render. useEffect separates "what to show" from "what to do after paint."

See it live — copy this example

Paste into src/App.jsx (or the file noted in the steps). Save and watch the browser update.

import { useState, useEffect } from 'react';

function UserProfile({ userId }) {
  const [user, setUser] = useState(null);

  useEffect(() => {
    fetch(`/api/users/${userId}`)
      .then(r => r.json())
      .then(setUser);
  }, [userId]);

  if (!user) return <p>Loading...</p>;
  return <h1>{user.name}</h1>;
}

Run Example »

Edit the code and click Run to see the result update live.

Code
Result

What happened?

  • The [userId] dependency array means: run again when userId changes.
  • Empty [] means run once on mount.

Try it yourself

  1. Fetch data in useEffect with a dependency array.
  2. Add cleanup: return () => clearInterval(id) for timers.
  3. Do not omit deps if you use props/state inside the effect.
  4. Change text or labels in the example and save — watch the browser update.
  5. Break the code on purpose (remove a bracket), read the error message, then fix it.

Remember

useEffect(fn, deps) for side effects after render. Include dependencies you use inside the effect. Return cleanup when needed.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
Incorrect dependencies in useEffect: Forgetting to include all necessary?
Short answer: dependencies can lead to bugs or infinite loops. Real-world example (ShopNest) ShopNest’s cart icon uses useState for count and useEffect to load the cart once on mount. Say this in the interview Define — o…
Mid PDF Detailed
React's useEffect Dependency Analysis:?
Short answer: If using hooks, make sure you are providing the correct dependencies to useEffect and useMemo, and avoid unnecessary recalculations. Real-world example (ShopNest) ShopNest’s cart icon uses useState for coun…
Mid PDF Detailed
State updates with closures: If using state in a function inside useEffect, make?
Short answer: sure to account for the most recent state. Real-world example (ShopNest) ShopNest’s cart icon uses useState for count and useEffect to load the cart once on mount. Say this in the interview Define — one cle…
Mid PDF Detailed
Overusing useEffect: Using useEffect for trivial things (e.g., directly?
Short answer: manipulating the DOM when it’s unnecessary) can lead to performance issues. Real-world example (ShopNest) ShopNest’s cart icon uses useState for count and useEffect to load the cart once on mount. Say this…
Junior PDF Detailed
What is useEffect and when do you use it?
Short answer: useEffect lets you run side effects in functional components (like data fetching, subscriptions, etc.). ✅ Syntax: useEffect(() =&gt; { // Side effect return () =&gt; { // Cleanup }; }, [dependencies]); ✅ Co…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

React.js Tutorial
Course syllabus

React.js Tutorial

Module 1: React Basics & Setup
Module 2: Props, Events & Lists
Module 3: Forms & Hooks
Module 4: Routing & Data
Module 5: State & Authentication
Module 6: Architecture & React 19
Module 7: Performance
Module 8: Full-Stack & Real-Time
Module 9: Testing & Deployment
Module 10: ShopCart Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details