Skip to main content

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​

Render is a calculation from immutable inputs
Inputs
props Β· state Β· context
UI description
JSX / React elements

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​

Not every render attempt commits
1Update triggers work
2Render calculates next UI
3React may restart / discard
4Chosen result commits
5Refs attach + Effects run later

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.

Why should this side effect happen?
User explicitly clicked/submitted/draggedEvent handler / Action
Committed UI must synchronize with an external systemEffect when needed
Only calculating JSX/derived dataKeep it in render with no side effect

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]);
A render owns a snapshot; updates create the next snapshot
Snapshot A
Do not mutate
Snapshot B
New value / identity

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.

Local mutation
Fresh local array/object created during this render and not shared.
Shared mutation
Props, state, module globals, cached shared objects, or values another render can observe.

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.

State and refs have different ownership contracts
State
Snapshot participates in rendering; setters schedule future renders.
Ref
Mutable imperative storage; changing .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.

React must remain the owner of component and Hook execution
Your code
JSX + custom Hook calls
React
Controls render order, state slots, retries, scheduling
Host UI

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.
Purity creates scheduling and optimization freedom
Pure restartable render
Retry
Interrupt
Server render
Memoize

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.

What kind of bug are you seeing?
Hydration differs server vs clientCheck nondeterministic render inputs
UI fails to update after mutationCheck state/prop immutability
Duplicate external side effectCheck side effects during render
State resets unexpectedlyCheck component type/key/position identity

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.

Summary​

Render should be restartable and discardable
Immutable inputs
JSX description
External world changes later

References​