Skip to content

Search

Articles, tools, solutions and knowledge base guides

Start typing to search across articles, tools and apps.

React Rendering and Reconciliation: How React Updates the UI

Does a note jump to a different product row after sorting? Component identity, rather than a Hook or DOM performance, may be the cause. Follow an update from an event to the commit, then build a list and an editor with predictable state behavior.

Published Written by Reading time 9 min read
On this page Contents: quick navigation between sections

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]

Design around data identity and explicit state ownership. Stable keys preserve row state across reordering; a deliberate key change resets an independent editor. Pure rendering lets React schedule work safely. Optimize only after checking correctness and measuring the actual cost.

React Rendering Reconciliation React Keys State TypeScript StrictMode Profiler

Frequently asked questions

Does every render change the DOM?

No. React can retain existing DOM nodes when the result requires no DOM changes. [1]

Why does a setter not change the local variable?

It requests a later render rather than changing the current invocation's snapshot. [2][3]

Can I sort props directly?

No. Array.sort mutates the array; sort a copy or use toSorted in a supported environment. [4]

Does identical JSX guarantee state preservation?

No. Element identity in the tree matters, not just how similar the JSX looks. [5][6]

Does a key preserve state after a row is removed?

No. Readding an unmounted row creates fresh state; keep persistent notes above the row or in a data store. [5][6]

When should I change a key deliberately?

When a new entity should start an independent local state lifecycle. [5]

Does a Transition speed up every calculation?

No. It changes update scheduling, not the calculation's cost or execution thread. [4][7]

Do two console logs mean two commits?

No. A log inside a component shows a function call, not a committed change. [8]

Does memo prevent every render?

No. It can skip some parent-driven renders, but state, context and other causes still matter. [9][10][11]

What should I test first?

User-visible behavior such as state, focus and data identity, then performance. [3][5][6][8][9]

Sources and references

  1. React, Render and Commit↩1↩2↩3
  2. React, State as a Snapshot↩1↩2
  3. React, Queueing a Series of State Updates↩1↩2↩3↩4
  4. React, Components and Hooks must be pure↩1↩2↩3↩4
  5. React, Preserving and Resetting State↩1↩2↩3↩4↩5↩6↩7↩8↩9↩10↩11
  6. React, Rendering Lists↩1↩2↩3↩4↩5↩6↩7↩8
  7. React, useTransition↩1↩2
  8. React, StrictMode↩1↩2↩3↩4
  9. React, Profiler↩1↩2↩3↩4↩5
  10. React, memo↩1↩2
  11. React, React Performance tracks↩1↩2

Was this helpful?

Get new articles by email

One short email per new Learning article. No spam, unsubscribe in one click.

We only use your email to send new articles. No third-party sharing.

Back to Learning