React Reference ยท Concepts and Pitfalls
You might not need an effect
A checklist of situations where useEffect is the wrong tool in React: derived data, resetting state, reacting to events, chained updates and notifying parents.
useEffect is an escape hatch for talking to systems outside React: the network, timers, subscriptions, the DOM of a third-party widget. Many components use effects for jobs that React can do better in plain render code or in event handlers. The usual cost is an extra render, a flash of wrong content, an infinite loop or a stale value.
A quick test: if there is no external system involved, you probably do not need an effect. Removing an unnecessary effect makes code shorter, faster and easier to follow, because data flows in one direction during render instead of bouncing through state.
// Instead of an effect that copies one state into another... useEffect(() => setVisible(items.filter(isOpen)), [items]); // ...compute it while rendering const visible = items.filter(isOpen);
Try it
The first component filters a list in an effect and stores the result in state. Every keystroke renders twice: once with a stale result, then again after the effect runs. The second computes the result during render. Compare the render counts in the console.
Another typical misuse is telling the parent about a change from an effect. The effect runs on mount too, so the parent hears about a "change" nobody made. Calling the parent from the handler that caused the change avoids that.
On load, the parent already logged "heard (effect): false" although the user did nothing.
Syntax
Common situations and what to do instead:
| You are tempted to... | Do this instead |
|---|---|
| Compute a value from props or state in an effect and store it | Calculate it during render (Derived state); use useMemo if it is expensive. |
| Reset all state when a prop changes | Give the component a key (Resetting state with keys). |
| Adjust part of the state when a prop changes | Compute it during render, or store an id instead of a copy. |
| Run logic because the user clicked or submitted | Put it in the event handler. |
| Chain effects that set state for each other | Compute everything in the handler that started the change, then set the state once. |
| Notify a parent after a state change | Call the parent's callback in the same handler, or lift the state up. |
| Pass data up from child to parent through an effect | Fetch in the parent and pass it down, or lift the state. |
| Subscribe to an external store | Use useSyncExternalStore. |
| Initialize app-wide singletons | Do it at module level, outside components. |
| Fetch data | An effect is fine, but add cleanup for race conditions, or use a framework or data library. |
Usage notes
- Effects are for synchronization. If the only thing an effect touches is React state, ask whether render could do it.
- A flash of stale UI after a prop change is the fingerprint of an effect that should be a render-time calculation.
- Effects that depend on other effects' state make a hidden chain: A sets B, B's effect sets C. Each step is another render. Replace the chain with one handler.
- Not all logic can be derived: user-visible results of a specific action, such as a toast after a purchase, belong in the handler (Effects vs event handlers).
- Document-level behavior is a valid use:
document.title, aResizeObserver, focus management, analytics page views. - Keep the lint rule on. eslint-plugin-react-hooks and the newer compiler rules flag several of these patterns, such as setting state synchronously inside an effect.
- React 19 and server frameworks give you better places for loading data (Suspense with
use, server components, loaders) than an effect in each component.