Skip to content

React useEffect and Fetching Data: Dependencies, Cleanup, Loading and Error States

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

Components should be pure: given the same props and state, they return the same JSX and change nothing outside themselves. But real apps must talk to the outside world: load prices from an API, start a timer, listen to the window size, save to localStorage. React gives you a place for that work, the useEffect Hook, which runs your code after React has updated the page. This lesson shows how effects work, how the dependency array decides when they run again, how cleanup stops timers and outdated requests, how to fetch data with proper loading and error states, and the common cases where an effect is the wrong tool.

Quick answer

Use useEffect(() => { ... }, [deps]) to synchronise a component with something outside React. The effect runs after the render that shows on screen. With [] it runs once after the component appears; with [city] it runs again whenever city changes; with no array it runs after every render. Return a cleanup function to undo the work, such as clearInterval or ignoring a stale response. To fetch data, keep data, loading and error in state, call fetch inside the effect, check response.ok, and set the state when the answer arrives.

40 min read and practice · Content checked on 22 November 2026

In this lesson you will

  • Explain what an effect is for and when it runs
  • Control re-runs with the dependency array and clean up after an effect
  • Fetch data with loading and error states and avoid race conditions
  • Recognise code that does not need an effect

What an effect is, and cleanup

Rendering must stay pure, so you cannot start a timer or call an API in the body of a component: React may render many times, and each render would start another one. An effect is code that React runs after it has put the result on screen. You write it as a function passed to useEffect. The question to ask is "what outside system does this component need to stay in sync with?" A clock needs a timer, a chat room needs a connection, a page title needs document.title. Those are effects.

Most effects start something that must be stopped later. Return a function from the effect and React calls it before running the effect again and when the component disappears from the page. This cleanup is where you call clearInterval, remove an event listener or close a connection. Without it, a component that is shown and hidden ten times leaves ten timers running. In development, if your app uses <StrictMode>, React deliberately runs every effect, its cleanup and the effect again once on mount, to prove that the cleanup is correct.

Try it: A clock with a timer and cleanup
Code
ResultUpdates as you type
Console
  1. Console messages, errors, form data and clicked links appear here.

The Console shows "start timer" once. Hide the clock and the cleanup logs "stop timer". Change the time zone and React stops the old timer and starts a new one, because zone is in the dependency array. Delete the return and hide and show the clock a few times: the old timers keep running in the background.

Sign in free to read the full lesson

You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.

Unlocks after sign-in:

  • 3 more sections with live code examples
  • Common mistakes to avoid
  • Hands-on coding exercise with automatic checks
  • Practice quiz and the graded lesson test
Sign in or create a free account

Free forever. No card, no paywall.

Questions people ask

Why does my useEffect run twice in development?

With <StrictMode>, React mounts each component, removes it and mounts it again in development, so every effect runs, cleans up and runs again. It checks that your cleanup works. It does not happen in production.

Can I use async and await inside useEffect?

Yes, but not on the effect function itself. Write async function load() { ... } inside the effect and call load(). The effect must return either nothing or a cleanup function, never a promise.

What is the difference between useEffect and an event handler?

An event handler runs because of a specific user action, such as a click. An effect runs because the component is on screen and must stay in sync with something. Put logic caused by an action in the handler.

Should I fetch data in useEffect in a Next.js app?

Usually not. Next.js lets server components fetch data before the page is sent, which is faster and better for search engines. useEffect fetching still fits data that depends on the browser, such as a user’s live choices.

How do I cancel a fetch when the component unmounts?

Create an AbortController in the effect, pass { signal: controller.signal } to fetch, and call controller.abort() in the cleanup. Ignore the AbortError that the aborted request throws.

Sources

Facts in this lesson were checked against these official pages on 22 November 2026.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.