How React works inside
React compares the new element tree with the old one and keeps state only where the type and the key stay the same. The work is split into small Fiber units, so the render phase can pause for more urgent updates, while the commit phase always runs in one go. Transitions, automatic batching, StrictMode and the React Compiler all come from these ideas. A specialist uses them to explain strange bugs and slow screens.
Author: bezzad
Contents
The problem: strange bugs no one can explain
In our shop admin, the team has two bugs that look like magic:
- An admin switches between two customers. The “Notes” field still shows the text typed for the first customer.
- A product form loses everything typed each time the “Advanced” checkbox is clicked.
And one slow screen: changing a tab in the order list freezes the page for half a second. To fix these, you must know how React decides what to keep, what to throw away, and when it does the work.
Render and commit
Every update has two phases:
- Render phase: React calls your components and gets a new tree of elements. Then it compares this tree with the previous one. Nothing on the screen changes yet. This phase must be pure: no side effects, no changing outside values.
- Commit phase: React applies the smallest set of DOM changes, then runs layout effects, and later the normal effects. The commit always runs in one go, so the user never sees half an update.
Reconciliation: same place, same type, same key
Comparing two trees perfectly is very expensive. React uses two simple rules instead:
- Different type means a new subtree. If a
<div>becomes a<section>, or<ProductForm>becomes<AdvancedForm>, React destroys the old one, with all its state, and creates a new one. - Same type at the same place means the same component. React keeps its state and only updates its props.
- Keys give an identity to a child in a list, or anywhere you want. Same key means the same component. A new key means a new component.
Now the two bugs are clear.
Bug 1: the notes stay. The page renders <CustomerNotes customer={selected} />. When the admin picks another customer, the type and the place are the same, so React keeps the component and its state. The fix is to say “this is a different customer” with a key:
<CustomerNotes key={selected.id} customer={selected} />
Bug 2: the form resets. The code defines a wrapper component inside the parent:
function ProductEditor({ advanced }: { advanced: boolean }) {
// Bad: a new component type on every render
const Wrapper = ({ children }: { children: React.ReactNode }) =>
advanced ? <fieldset>{children}</fieldset> : <div>{children}</div>;
return <Wrapper><ProductForm /></Wrapper>;
}
Each render creates a new function, so Wrapper is a new type each time. React destroys ProductForm and its state on every render. Define components at the top level of a file, never inside another component.
Fiber: work in small units
Before React 16, a render ran from start to end in one call stack. A big tree could block the browser for a long time. Fiber is the rewrite that changed this:
- Each component instance is a fiber: a small object with its type, props, state, and links to its parent, child and sibling.
- React walks the fiber tree one unit at a time. Between units, it can check if something more urgent arrived, like a key press.
- React keeps two trees: the current one (on the screen) and the work-in-progress one. When the work is done, the commit makes the new tree current.
Because of this, the render phase can be paused, restarted or thrown away. That is why render must be pure: React may call your component more than once for one update.
Concurrent rendering and priorities
React gives every update a priority (internally called a lane):
- Urgent updates: typing, clicking, pressing. The user expects to see them at once.
- Transitions: updates that may take time and can wait, like showing a new tab or a filtered list.
import { useState, useTransition } from 'react';
function OrderTabs() {
const [tab, setTab] = useState<'open' | 'shipped'>('open');
const [isPending, startTransition] = useTransition();
function selectTab(next: 'open' | 'shipped') {
startTransition(() => setTab(next));
}
return (
<>
<TabButtons active={tab} onSelect={selectTab} dim={isPending} />
<OrderList status={tab} /> {/* slow: 3,000 rows */}
</>
);
}
- The click is handled at once. The slow list renders as a transition.
- If the user clicks another tab while the list is rendering, React drops the old work and renders the newest tab. No time is wasted on a screen nobody will see.
- isPending is true while the transition is waiting, so you can dim the old content.
- A transition does not make the list faster. It only stops the list from blocking urgent work. To make it faster, render fewer rows (pagination or virtualization) or avoid useless renders.
- useDeferredValue is the same idea when you do not own the setter: it gives you a value that “lags behind” during slow renders.
Automatic batching
Since React 18, all state updates in the same event or the same task are batched into one render, also inside promises, timers and native event handlers.
async function save() {
const order = await api.save();
setSaving(false);
setOrder(order); // one render, not two
}
If you really need the DOM to update in the middle (for example to measure it), flushSync forces it. This is rare and slow, so use it only when needed.
StrictMode: finding impure code
In development, StrictMode on purpose:
- Calls components twice in render, to find side effects in render.
- Runs effects, cleanup, then effects again on mount, to find a missing cleanup.
This happens only in development. If your app breaks because of it, the bug is real: React may also re-run render during concurrent rendering, and the Activity component (React 19.2) can hide a part of the tree and show it again later, which runs the effect cleanup and the effect again.
The React Compiler
Manual useMemo, useCallback and memo are easy to get wrong. The React Compiler (stable since October 2025) is a build plugin that adds memoization for you, at the level of single values and JSX parts.
- It works only if your code follows the rules of React: pure render, no mutation of props or state, and the rules of hooks.
- If it finds code that breaks the rules, it skips that component and leaves it as it was.
- It does not fix slow work inside a render, like filtering 50,000 items. It only skips work that did not need to run again.
Common mistakes
| Mistake | Result | Better way |
|---|---|---|
| Same component type for a different item | Old state shows for the new item. | Give it a key, like the item id. |
| Defining a component inside a component | State is lost on every render. | Define components at the top level. |
| Side effects in render | Bugs with StrictMode and concurrent rendering. | Keep render pure. Use effects or event handlers. |
| Typing state inside a transition | The input itself feels slow. | Keep the input urgent, put the slow part in the transition. |
| Expecting a transition to make work faster | The screen is still slow, only not blocking. | Render less: pagination, virtualization, memo. |
| Turning off StrictMode to hide double calls | Real cleanup bugs stay hidden. | Fix the cleanup or the impure code. |
Summary in 7 lines
- Render builds and compares the tree and must be pure. Commit changes the DOM in one go.
- Same type and same key at the same place keeps state. A new type or a new key starts fresh.
- Use a key to reset a component for a new item. Never define a component inside another.
- Fiber splits the work into units, so React can pause, restart or drop a render.
- Transitions and useDeferredValue mark slow updates so urgent input stays fast.
- Updates are batched into one render, even after await.
- StrictMode finds impure code in development, and the React Compiler memoizes code that follows the rules.