React Reference ยท Rules of React
Where side effects belong
Where to put side effects in React: event handlers first, useEffect for synchronizing with outside systems, and never in the render body. With a flow example.
A side effect is anything a function does besides computing its return value: changing a variable outside it, updating the DOM by hand, sending a request, starting a timer, writing to storage, logging to a server. React components may have them, but only in the right places, because the render body must stay pure.
There are three places, in order of preference. Event handlers run because the user did something (a click, a key press); that is where most effects belong. Effects (useEffect, useLayoutEffect) run after React has put the result on screen, and are for keeping an outside system in sync with what is rendered, such as a subscription or a chart library. Everything else, such as the function body that returns JSX, must not have side effects.
function Search({ query }) { // render: only calculate const label = 'Results for ' + query; // effect: sync with an outside system after render useEffect(() => { document.title = label; }, [label]); // event handler: respond to a user action function handleSave() { sendToServer(query); } return <button onClick={handleSave}>{label}</button>; }
Try it
Syntax
A quick decision guide:
| The code runs because... | Put it in |
|---|---|
| The user clicked, typed, submitted, dragged | Event handler |
| The component appeared or a value it shows changed, and an outside system must match | useEffect (with cleanup) |
| Layout must be measured before the browser paints | useLayoutEffect |
| A value can be calculated from props and state | Render body (no effect at all) |
| An external store changes | useSyncExternalStore |
| A server needs to be called when the page is shown | A framework loader, a data library, or use() with a cached promise |
Edge cases: an effect that only copies state to other state is almost always a mistake; compute the value during render instead.
Usage notes
- Handlers can be impure. They do not run during render, so mutation, requests and navigation are normal there.
- Effects need cleanup for anything that persists: timers, listeners, subscriptions, connections. Strict Mode runs setup, cleanup and setup again in development to check this.
- Dependencies describe the sync. List every reactive value used inside; omitted values cause stale reads. See useEffect dependencies.
- Not everything needs an effect. Deriving data, resetting state on a prop change (use a key) and sharing logic between handlers do not.
- Initialization that runs once per app (analytics setup, SDK init) belongs at module level or in a guarded block, not in render.
- Server rendering never runs effects, so an effect is a safe place for code that needs
window. - Idempotent render side effects are tolerated only in rare cases (like setting a ref lazily), and they must give the same result however often they run.