Purity, immutability, and render safety
The Rules of React are correctness constraints, not optional style advice.
They matter because React may render more than once, interrupt or abandon work, render on the server, hydrate on the client, and automatically memoize code with React Compiler.
Components and Hooks must be pureβ
For the same inputs, rendering should produce the same result. React calls this idempotence.
Bad:
function ClockLabel() {
const now = Date.now();
return <span>{now}</span>;
}
Date.now(), Math.random(), and similar changing values during render can create retry differences, hydration mismatches, broken memoization assumptions, and unpredictable tests.
Render and commit are different phasesβ
Your component function participates in render. External mutation belongs outside render.
Side effects must not run during renderβ
Bad:
function AnalyticsCard({ event }) {
analytics.track(event);
return <Card />;
}
If React retries or abandons that render, the external effect already happened.
Props and state are immutable snapshotsβ
Never mutate a prop:
function UserCard({ user }) {
user.name = 'Changed'; // wrong
return <p>{user.name}</p>;
}
Create a new value instead:
const displayUser = {
...user,
name: user.name.toUpperCase(),
};
State follows the same rule.
Bad:
items.push(newItem);
setItems(items);
Good:
setItems(items => [...items, newItem]);
Mutating old snapshots behind React's back breaks reasoning about identity and makes concurrent/retried work unsafe.
Hook arguments and return values should be treated as immutableβ
Values passed into Hooks participate in Hook contracts. Do not mutate values after passing them to a Hook when that Hook may rely on their identity/content.
Likewise, do not mutate data returned by Hooks unless the API explicitly documents it as mutable, such as a ref object's .current outside render-sensitive usage.
Values should not be mutated after being used in JSXβ
Once a value has contributed to a React element description, treat that render's value as fixed.
const styles = { color: 'red' };
const element = <div style={styles}>Hello</div>;
// mutating styles after this point creates ambiguous ownership
Prefer constructing the final value before creating JSX.
Local mutation can be safeβ
Mutation created and consumed entirely inside one render is often fine because no previous render or external owner can observe it.
function List({ items }) {
const rows = [];
for (const item of items) {
rows.push(<li key={item.id}>{item.name}</li>);
}
return <ul>{rows}</ul>;
}
The array is new for this render and does not mutate props/state/shared module data.
Refs are an escape hatch, not render stateβ
Refs are mutable, but render should not depend on arbitrary reads/writes of .current as if it were reactive state.
.current does not schedule render.Use refs for DOM nodes, timers, external instances, and other imperative valuesβnot hidden UI state.
Stable component identity mattersβ
Do not define a component inside another component's render just to create it dynamically:
function Parent() {
function Child() {
return <div />;
}
return <Child />;
}
That creates a new component type during each render and can reset state or confuse optimization.
Prefer module-scope component definitions.
React calls Components and Hooksβ
Call components through JSX and Hooks through normal Hook composition. Do not invoke component functions manually as arbitrary utilities.
Why purity unlocks modern Reactβ
Purity is what allows React to safely:
- retry rendering;
- interrupt low-priority work;
- abandon obsolete renders;
- prerender/server-render deterministically;
- hydrate matching output;
- memoize with React Compiler;
- run Strict Mode development checks.
Common mistakesβ
- calling changing APIs such as
Date.now()directly during render; - mutating props or state;
- writing localStorage/network/analytics during render;
- using refs as hidden reactive state;
- defining component types dynamically in render;
- manually calling component functions;
- assuming render executes once and therefore side effects are safe.
Debugging ruleβ
When behavior seems impossible, ask whether some code broke the snapshot model or mutated the outside world during render.
Interview questionsβ
Junior: What does it mean for a React component to be pure?
Mid-level: Why is mutating state directly incompatible with React's snapshot model?
Senior: Explain how purity enables concurrency, SSR/hydration, Strict Mode, and React Compiler optimization.