useEffect is a React hook that runs code after a component renders, so you can synchronise the component with something outside React, such as a network request, a timer, a browser API or a third-party widget. By default the effect runs after every render; you control that with a dependency array. The useeffect hook runs only in the browser after the screen has updated, never during server rendering.
This guide explains what an effect is, when React runs it, and how to tell an effect apart from ordinary event-handler code. The examples use React 19 with function components.
Rendering must be pure: given the same props and state, a component returns the same JSX and does nothing else. Anything that reaches outside the component is a side effect. Examples are fetching data, subscribing to a WebSocket, changing document.title, starting a timer or measuring a DOM node.
useEffect is the place for side effects that happen because the component is shown or because some value changed. Effects caused by a user action, such as submitting a form, belong in the event handler instead.
You pass useEffect a function (the setup). React calls it after it has rendered the component and updated the DOM. Here the title is updated after every click.
The timing follows a fixed sequence:
So an effect runs after paint in most cases, which keeps it from blocking the screen. If you must measure layout before the browser paints, useLayoutEffect exists for that, but you rarely need it.
How often it runs depends on the second argument:
| Call | When it runs |
|---|---|
useEffect(fn) | After every render |
useEffect(fn, []) | After the first render only (plus the Strict Mode extra run in development) |
useEffect(fn, [a, b]) | After the first render and whenever a or b changed |
The effect runs on mount and again whenever userId changes. The function it returns is the cleanup, which cancels the previous request before the next one starts. Cleanup is covered in a related article, but note that it exists.
In development, when your app is wrapped in <StrictMode>, React mounts, unmounts and mounts each component again to check that your effect and its cleanup are symmetric. You will see setup, cleanup, setup on first load. This does not happen in production builds. If the double run breaks your code, the effect usually lacks proper cleanup.
Many uses of useEffect are better done another way:
key on the component.No. Effects run only in the browser, after the component has been committed to the DOM. Server rendering calls the component body but skips effects.
Not exactly. With an empty dependency array it resembles componentDidMount plus cleanup on unmount, but effects are designed around synchronising with values, not around lifecycle moments.
The effect function itself cannot be async, because it must return either nothing or a cleanup function, not a promise. Define an async function inside it and call it.
In development, Strict Mode intentionally re-runs effects once to expose missing cleanup. Production builds run it once.
useEffect runs after React has rendered and committed, to synchronise a component with outside systems. The dependency array decides when it runs again, the returned function cleans up, and many tasks that look like effects are better solved in render or in event handlers.