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.
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.
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.
JSX → DOM
User sees UI
Side effect executes
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.
Empty array
Run once on mount. Never re-run. Perfect for initial data fetching.
With dependencies
Run on mount + whenever dep changes. Great for search/filter updates.
No array
Run after every render. Rarely what you want — use with caution.
[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.
Effect runs → timer starts
Cleanup runs first → effect runs again
Cleanup runs → timer cleared
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.
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
Async directly in useEffect
Stale closures
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 #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.
Test your knowledge
[] in useEffect mean?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 →