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.
Console
- 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
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.
- React docs: Synchronizing with Effects
- React reference: useEffect
- React docs: You Might Not Need an Effect
- MDN: Using the Fetch API
- Frankfurter: free exchange rate API (ECB data)
- UAE Central Bank: dirham peg to the US dollar
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.