Render and commit
Rendering calculates a tree from props, state and context. A state update schedules work; React may call a component and its descendants, then apply the required DOM changes during commit. Calling a component does not imply a DOM mutation or remount. Layout and paint are browser responsibilities. [1]
State snapshots and batching
quantity.tsx
import { useState } from "react";
export function QuantityPicker() {
const [quantity, setQuantity] = useState(0);
function addThree() {
setQuantity((previous) => previous + 1);
setQuantity((previous) => previous + 1);
setQuantity((previous) => previous + 1);
}
return (
<section aria-label="Quantity">
<output>{quantity}</output>
<button type="button" onClick={addThree}>
Add three
</button>
</section>
);
}
An event handler sees the state of the render that created it. Three calls to setQuantity(quantity + 1) using the same snapshot do not add three. Functional updaters are processed in order; each click in the example adds three. Updaters must be pure. Batching reduces work but does not promise a particular number of component calls. [2][3]
Pure rendering and immutable inputs
products.ts
export interface Product {
readonly id: string;
readonly name: string;
readonly priceCents: number;
}
export function sortProducts(
products: readonly Product[],
): Product[] {
return [...products].sort((first, second) => {
return first.priceCents - second.priceCents;
});
}
Do not mutate inputs, write to external storage or issue requests during ordinary component rendering. React may repeat or discard that work. sortProducts copies the array before sorting and only reads the records. The example assumes valid domain data: integer prices in cents and unique stable IDs. External side effects belong in event handlers or appropriately designed effects. [4]
Reconciliation and component identity
Reconciliation matches elements across successive trees. State preservation depends on component type and tree position, including keys among siblings. A changed type or key can reset state and remount a subtree. This is not a claim that React finds a globally minimal edit sequence; implementation details are not public API contracts. [5][6]
If typed input disappears despite an unchanged ID, check for a changed parent, component type or conditional subtree. A stable key does not transfer state between different parents. [5]
Stable keys in lists
product-list.tsx
import { useState } from "react";
import { sortProducts } from "./products";
import type { Product } from "./products";
interface ProductListProps {
products: readonly Product[];
}
interface ProductRowProps {
product: Product;
}
function ProductRow({ product }: ProductRowProps) {
const [note, setNote] = useState("");
return (
<li>
<label>
{product.name}
<input
value={note}
onChange={(event) => setNote(event.target.value)}
/>
</label>
</li>
);
}
export function ProductList({ products }: ProductListProps) {
const [sortByPrice, setSortByPrice] = useState(false);
const visible = sortByPrice ? sortProducts(products) : products;
return (
<section aria-label="Products">
<button
type="button"
onClick={() => setSortByPrice((previous) => !previous)}
>
{sortByPrice ? "Original order" : "Sort by price"}
</button>
<ul>
{visible.map((product) => (
<ProductRow key={product.id} product={product} />
))}
</ul>
</section>
);
}
ProductList changes the order without mutating its data. ProductRow owns a local note; key={product.id} keeps it attached to the row during reordering under the same parent. Index keys are risky when sorting, inserting or deleting, while random keys created on each render destroy identity. Keys need to be unique among siblings, not globally, and are not ordinary props. Removing a row destroys its local state. [5][6]
Intentional editor resets
editor.tsx
import { useState } from "react";
interface DocumentRecord {
id: string;
title: string;
}
interface EditorProps {
document: DocumentRecord;
}
function Editor({ document }: EditorProps) {
const [draft, setDraft] = useState(document.title);
return (
<label>
Title
<input
value={draft}
onChange={(event) => setDraft(event.target.value)}
/>
</label>
);
}
export function DocumentPanel({ document }: EditorProps) {
return <Editor key={document.id} document={document} />;
}
DocumentPanel uses the document ID as the editor key. Switching documents resets the draft, whereas a title update for the same document does not overwrite it. This is an intentional contract; applications should warn before losing unsaved changes. Avoid declaring component functions inside other components because a new function becomes a new type on each render. [5]
Priorities and interrupted work
Rendering can be interrupted or restarted, for example for updates marked as Transitions. useTransition marks less urgent view updates; it cannot control a text input's value. It does not move a heavy synchronous function to another thread or replace a Web Worker. Commit applies prepared work, so render counts are not committed update counts. [4][7]
StrictMode and debugging
In development, StrictMode performs extra checks for render purity and effect and ref lifecycles. Extra calls do not imply double production rendering; the checks depend in part on where StrictMode is placed in the tree. Do not remove it merely to hide duplicate subscriptions. Verify that resource setup and cleanup are symmetrical. [8]
Measure rather than guess
profiled-list.tsx
import { Profiler } from "react";
import type { ProfilerOnRenderCallback } from "react";
import { ProductList } from "./product-list";
import type { Product } from "./products";
interface ProfiledListProps {
products: readonly Product[];
report: ProfilerOnRenderCallback;
}
export function ProfiledList({ products, report }: ProfiledListProps) {
return (
<Profiler id="products" onRender={report}>
<ProductList products={products} />
</Profiler>
);
}
Profiler calls onRender after a profiled subtree commits an update. actualDuration measures rendering time, baseDuration estimates unoptimized rendering cost, and commitTime helps correlate measurements. None directly measures browser paint or INP. Use React DevTools and the Performance panel, compare equivalent interactions and account for profiling overhead. memo is an optimization, not a guarantee; state and context changes can still trigger rendering. [9][10][11]
Regression tests and decisions
Related articles:
Mount QuantityPicker, click twice and expect 3 then 6. Enter distinct notes in ProductList rows, reorder them and check their IDs. Remove and restore a row; its note should reset. Switch the DocumentPanel ID and verify a reset; for the same ID the draft should remain. Repeat under StrictMode. Establish correctness before measuring and deciding whether to narrow state scope, memoize or virtualize. [3][5][6][8][9]
