React Reference ยท Rules of React
Components must be pure
The first rule of React: a component's render must be a pure calculation from props, state and context. Why impurity breaks, and how StrictMode exposes it.
React asks one thing of every component function: given the same props, state and context, return the same JSX, and do nothing else while doing so. A function like that is called pure. Rendering is a calculation, like a formula in a spreadsheet cell, not an action. React may run it more than once, pause it, throw the result away, or run it ahead of time while you are still typing, and all of that is safe only if the function has no side effects and does not depend on hidden inputs.
Two things make a component impure. It can change something that existed before the render began, such as a variable outside the component or an object it received. Or it can read something that changes by itself, such as Math.random(), Date.now() or a mutable module variable, and use it to decide the output.
let guests = 0; function Cup() { guests = guests + 1; // impure: changes an outside variable return <p>Guest {guests}</p>; // output depends on how often React rendered }
Try it
In the first example the console shows that ImpureCup ran twice for one mount: StrictMode calls each component function twice in development. A pure component gives the same result both times; the impure one drifts, which is why the first cup announces "guest 2" instead of "guest 1". The second example mutates rows and total, but they were created inside the render, so no earlier render can observe the change. That is called local mutation and is always fine.
Syntax
There is no API for this rule; it is a contract. The practical checklist:
| Do | Do not |
|---|---|
| Compute JSX from props, state and context | Change variables or objects that existed before the render |
| Create and mutate new arrays/objects inside the function | Mutate props, state or context values |
Put side effects in event handlers or useEffect |
Fetch, subscribe, write the DOM or log analytics while rendering |
| Read time or randomness in an effect, an event handler or state initializer you control | Call Math.random(), Date.now() or new Date() directly to shape the output |
Edge cases: reading a constant defined at module level is fine. Reading a module variable that other code changes is not.
Usage notes
- Why it matters: concurrent rendering, Suspense, transitions and the React Compiler all rely on being able to call or skip your component freely.
- StrictMode is the detector. Double rendering in development, plus an extra effect setup/cleanup cycle, surfaces most violations. Keep it on.
- Order independence. Components should not rely on being rendered before or after a sibling; each reads only its own inputs.
- Pass values down instead of sharing them sideways. If two components need the same number, compute it in the parent and pass it as a prop.
- Logging is the common exception.
console.login render is harmless in practice, but it will print twice in StrictMode. - Where does changing things go? Respond to the user in handlers; synchronize with the outside world in effects. See side effects.