1. What are side effects

React components have one main job: take props and state, return JSX. That's the render. But real apps need to do things outside of rendering — talk to servers, start timers, read from localStorage, change the page title. These are called side effects.

🛒
UrbanCart Example
Side effects on the product page

When an UrbanCart customer opens the product page, the component needs to fetch products from the API (side effect), update the document title to "UrbanCart — 194 Products" (side effect), and start a countdown timer for a flash sale (side effect). None of these are part of rendering JSX — they interact with the outside world.

✅

Pure render (not a side effect)

Computing JSX from props/state: return <h1>{title}</h1>. Predictable, same input = same output.

⚡

Side effect

Anything that reaches outside the component: API calls, timers, DOM manipulation, localStorage, subscriptions.

Rule of thumb: If it touches something outside your component's scope — the network, the browser DOM, a timer, storage — it's a side effect and belongs in useEffect.

2. The useEffect hook

useEffect tells React: "After you finish rendering, run this code." It takes a callback function that contains your side-effect logic, and an optional dependency array that controls when the effect re-runs.

useEffect-basic.jsx Basic Syntax
useEffect Timing
Component renders
JSX → DOM
→
Browser paints screen
User sees UI
→
useEffect runs
Side effect executes
Key insight: useEffect runs after paint, not before. The user sees the UI first, then the effect kicks in. This keeps the initial render fast.

3. The dependency array

The second argument to useEffect is an array of values React watches. When any value in the array changes between renders, the effect re-runs. This is how you control when your side effect fires.

1️⃣
useEffect(fn, [])

Empty array

Run once on mount. Never re-run. Perfect for initial data fetching.

🔄
useEffect(fn, [dep])

With dependencies

Run on mount + whenever dep changes. Great for search/filter updates.

♾️
useEffect(fn)

No array

Run after every render. Rarely what you want — use with caution.

dependency-examples.jsx All Three Patterns
UrbanCart pattern: When UrbanCart's category filter changes from "Electronics" to "Clothing", the effect with [category] re-runs and fetches new products. The effect with [] only ran once on initial page load.

4. The cleanup function

When your effect creates something ongoing — a timer, a subscription, an event listener — you need to clean it up when the component unmounts or before the effect re-runs. Return a function from useEffect to do this.

cleanup-timer.jsx Flash Sale Countdown with Cleanup
Cleanup Lifecycle
Mount
Effect runs → timer starts
→
Re-render (if deps change)
Cleanup runs first → effect runs again
→
Unmount
Cleanup runs → timer cleared
Memory leaks: Without cleanup, timers and subscriptions keep running after the component is removed from the DOM. This is the #1 source of memory leaks in React apps. Always clean up intervals, timeouts, event listeners, and WebSocket connections.

5. Fetching data with useEffect

The most common use of useEffect is loading data when a component mounts. The pattern always includes three states: loading, data, and error.

fetch-products.jsx UrbanCart Data Fetching Pattern
📋
The Loading-Data-Error Pattern
Every data-fetching component uses this

Step 1: Set loading = true initially. Show a spinner or skeleton.
Step 2: In useEffect, fetch data. On success, store data and set loading = false.
Step 3: On failure, store the error message and set loading = false.
Step 4: In JSX, check loading first, then error, then render data.

6. Common pitfalls

useEffect trips up beginners in predictable ways. Here are the three most common mistakes — and how to avoid them.

Infinite loops: missing dependency array

infinite-loop.jsx Bug: Infinite Re-renders

Async directly in useEffect

async-pitfall.jsx Bug: async Callback

Stale closures

stale-closure.jsx Bug: Stale Value in Timer
Summary of rules: (1) Always include a dependency array unless you have a good reason not to. (2) Never make the useEffect callback async — define an inner function instead. (3) Use the functional form of setState inside intervals and timeouts.

Try It Yourself #1

Fetch UrbanCart products on mount from the dummyjson API, show a loading spinner while waiting, then display product cards. Edit the code and click Run to see it live.

🛠️ Try It Yourself — Fetch UrbanCart Products
Output

Try It Yourself #2

Build two things: a document title updater that shows the item count, and a countdown timer with proper cleanup. Watch the tab title change and the timer tick down.

🛠️ Try It Yourself — Title Updater + Countdown Timer
Output

Test your knowledge

Question 1 of 5
What is a "side effect" in React?
Question 2 of 5
What does an empty dependency array [] in useEffect mean?
Question 3 of 5
Why do you return a function from useEffect?
Question 4 of 5
Why can't you pass an async function directly to useEffect?
Question 5 of 5
What causes an infinite loop when using useEffect for data fetching?
0
/5
Great job!

Up Next

Lesson 18: Lists, Keys & Conditional Rendering →
You can now fetch data and manage side effects. Next: rendering dynamic lists and toggling UI with conditions.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You just mastered useEffect.
Now build production React apps.

Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses →