Full answer
useEffect is for work that connects a component to something outside React: the browser window, a timer, a subscription or a network service. It runs after the render has been committed, so it does not block painting.
The second argument controls when it runs. Without an array, the effect runs after every render. With [], React runs it after the first render only. With [a, b], it runs after the first render and again after any render where a or b changed, compared with Object.is.
The array should list every reactive value (props, state, and variables derived from them) that the effect reads. Leaving a value out does not make the effect smarter. It makes it read stale values. The eslint rule react-hooks/exhaustive-deps warns about this, and the answer is almost always to fix the code, not to disable the rule.
Cleanup is the function you return. React calls it before running the effect again and when the component unmounts. If the effect added an event listener, the cleanup removes it. If it started a timer, the cleanup clears it. Without cleanup, listeners pile up, memory leaks and old callbacks run with outdated data.
In development, StrictMode runs each effect, its cleanup, and the effect again once after mount. This is a test that your cleanup mirrors your setup. It does not happen in production.
Not everything belongs in an effect. A value calculated from props can be computed during render. Code that answers a user click belongs in the event handler. Effects are for synchronising.
The example tracks the window width. Open and close the listener once per mount, not per render.
import { useEffect, useState } from 'react';
function WindowWidth() {
const [width, setWidth] = useState(window.innerWidth);
useEffect(() => {
function handleResize() {
setWidth(window.innerWidth);
}
window.addEventListener('resize', handleResize);
console.log('listener added');
return () => {
window.removeEventListener('resize', handleResize);
console.log('listener removed');
};
}, []);
return <p>Width: {width}</p>;
}
export default function App() {
const [visible, setVisible] = useState(true);
return (
<div>
<button onClick={() => setVisible(false)}>Hide</button>
{visible && <WindowWidth />}
</div>
);
}The check shrinks the window to 800 pixels, then clicks Hide, which unmounts WindowWidth and triggers the cleanup. Console output and markup (jsdom starts with a width of 1024): listener added after resize: <div><button>Hide</button><p>Width: 800</p></div> listener removed
Key points
- The dependency array controls when the effect re-runs, based on Object.is comparison.
- List every reactive value the effect reads. Stale closures are the cost of leaving one out.
- Cleanup runs before the next effect run and on unmount.
- StrictMode runs an extra setup-cleanup-setup cycle in development only.
- Derived values and event reactions do not belong in effects.
Common mistakes
- Leaving out dependencies and reading stale state inside the effect.
- Forgetting cleanup, which adds a new listener on every mount.
- Using an effect to compute a value that could be calculated during render.
- Treating [] as "componentDidMount" and ignoring changing props.
- Making an effect async directly, because an effect must return a cleanup function or nothing, not a promise.
Follow-ups
- What happens if an object created in render is listed as a dependency?
- How would you fetch data inside an effect and avoid setting state after unmount?
- What is useEffectEvent for?
Tips
- Describe the three array forms briefly, then spend your time on cleanup and stale values.
- Mention StrictMode when asked "why does my effect run twice".
- Say that effects synchronise with the outside, and name one concrete outside system.