Sub topic 1
Urgent and Non-Urgent Updates
React 18 added a way to tell React that some state updates matter more than others. This chapter explains that idea: what makes an update urgent, what makes one non-urgent, and why the split fixes a class of performance problems that faster code often can't. Later chapters cover the APIs in detail. Here you build the mental model they rely on.
Prerequisites
This chapter assumes you already have:
- Solid knowledge of React function components and JSX.
- Experience with
useStateand props. - A working idea of how React re-renders a component tree: a state change re-renders the component that owns the state and, by default, all of its children.
- Optionally, a basic understanding of
React.memoand Suspense. You will seememoonce in this chapter, and it is explained where it appears.
You also need React 18 or later, with the app rendered through createRoot. A later chapter covers that setup.
The Problem: Every Update Has the Same Priority
Consider a search box that filters a long list. Each keystroke updates state, React re-renders, and the browser paints the result. When rendering is cheap, you never notice the steps in between.
When one part of the tree is expensive to render, the picture changes. The following component simulates that cost by making each list item busy for about one millisecond:
import { useState } from 'react';
function SlowItem({ text }) {
const start = performance.now();
while (performance.now() - start < 1) {
// Busy-wait to simulate an expensive render.
}
return <li>{text}</li>;
}
export default function App() {
const [text, setText] = useState('');
const items = [];
for (let i = 0; i < 200; i++) {
items.push(<SlowItem key={i} text={text} />);
}
return (
<>
<input value={text} onChange={(e) => setText(e.target.value)} />
<ul>{items}</ul>
</>
);
}Each keystroke re-renders 200 items at roughly 1 ms each, so about 200 ms of work. During that time the browser cannot process the next keystroke or paint the new character. Typing feels sticky.
The input is not the slow part. Its work is trivial. It is slow because it shares one update with the list, and a single update is rendered as one unit. React renders the whole result before it shows any of it.
Two Kinds of Updates
React 18 sorts updates by how the user experiences them.
- Urgent updates reflect direct interaction. The user expects an immediate response: typing, clicking, pressing, dragging. If these lag, the interface feels broken.
- Non-urgent updates (also called transition updates) move the UI from one view to another. The user expects some delay, and they would accept the old view staying on screen for a moment: filtering a large list, switching to a heavy tab, navigating to a new screen.
The table below shows how common cases usually fall.
| Interaction | Update | Priority |
|---|---|---|
| Typing in a text field | The text shown in the field | Urgent |
| Typing in a search field | The list of results below it | Non-urgent |
| Clicking a tab button | Highlighting the clicked button | Urgent |
| Clicking a tab button | Rendering a slow tab panel | Non-urgent |
| Pressing a toggle | The toggle's on/off look | Urgent |
| Moving a slider | The slider thumb position | Urgent |
| Moving a slider | A chart computed from the value | Non-urgent |
The same event often causes both kinds. In the search example, one keystroke produces an urgent update (the field must show the character) and a non-urgent one (the results can catch up). The performance problem above came from treating both as urgent.
Which Updates Are Urgent by Default
In React 18 and later, every state update is urgent unless you mark it otherwise. Your existing code keeps its old behavior. You opt in to non-urgent updates one update at a time, using startTransition or useTransition, which later chapters cover. You do not set priority on a component or a piece of state. You set it on the individual update, at the call site that triggers it.
What Changes When an Update Is Non-Urgent
Marking an update non-urgent changes how React renders it. An urgent render runs to the end without stopping. A non-urgent render can be interrupted. If a new urgent update arrives while React is partway through rendering a non-urgent one, React pauses it, handles the urgent update first, and then goes back to the non-urgent work. The next chapter explains how this works. For now, the observable effects matter more.
Here is the earlier example with the split applied. The preview uses startTransition, which is the plain function form of the API:
import { useState, startTransition, memo } from 'react';
const SlowList = memo(function SlowList({ text }) {
const items = [];
for (let i = 0; i < 200; i++) {
items.push(<SlowItem key={i} text={text} />);
}
return <ul>{items}</ul>;
});
function SlowItem({ text }) {
const start = performance.now();
while (performance.now() - start < 1) {
// Busy-wait to simulate an expensive render.
}
return <li>{text}</li>;
}
export default function App() {
const [text, setText] = useState('');
const [query, setQuery] = useState('');
function handleChange(e) {
const value = e.target.value;
setText(value); // Urgent: the field must show what was typed.
startTransition(() => {
setQuery(value); // Non-urgent: the list can catch up.
});
}
return (
<>
<input value={text} onChange={handleChange} />
<SlowList text={query} />
</>
);
}Two states now hold what used to be one value. text drives the field and query drives the list.
When the user types, React runs two renders for the same event. The first handles setText and renders at urgent priority. During this render query still has its old value, so SlowList receives unchanged props and memo lets React skip it. The field updates and paints within a frame. The second render handles setQuery, runs at lower priority, and can be interrupted. If the user types another character before it finishes, React throws away the unfinished render and starts again with the newest value.
The memo wrapper is not optional here. Without it, the urgent render would re-render SlowList anyway, because a parent render re-renders its children by default. The 200 ms of work would be back inside the urgent render, and the split would gain nothing.
The Timeline
The diagram shows two quick keystrokes arriving while the list is still rendering.
Notice that the list never showed a result for a alone. The intermediate state was skipped because a newer one superseded it.
Consequences You Should Expect
A few behaviors follow directly from this model. You will meet each again in later chapters.
- Intermediate states may never appear. React renders and commits only the latest transition state, so a fast typist may see the list jump from empty to the result for the full word. This is usually what you want, but do not write code that depends on seeing every value.
- The UI is never half-updated. React commits a transition render only after it finishes. Until then, the old content stays on screen, and the user never sees a mixture of old and new content from one update.
- Total work does not shrink. A transition does not make rendering faster. The 200 items still cost 200 ms. The gain is that the cost no longer blocks urgent updates, and that obsolete work can be abandoned.
- The old UI stays visible, so users may need a hint. While the transition renders, the screen shows stale content. Later chapters show how to detect this and signal it.
- Urgent updates always win. Even a low-priority render in progress gives way to a click or a keystroke. Transition renders can therefore be delayed by constant urgent updates, which is the intended trade.
Choosing Which Update Is Which
When you have a slow interaction, ask these questions in order:
- What must respond instantly because the user is touching it directly? That update is urgent. Leave it as is.
- What results from the interaction but can lag by a moment? That update is a candidate for a transition.
- Is the slow work caused by a state update you control? If it is, a transition applies. If the delay comes from something else, such as a slow network request or a long synchronous calculation inside an event handler, a transition will not help.
A good test is to imagine the update arriving a quarter of a second late. If the user would think the app is broken, the update is urgent. If they would barely notice, it can be a transition.
Measuring Before You Split
Priorities are a response to a measured problem, not a default style. Before reaching for them, confirm that a render is actually slow.
- Use the React DevTools Profiler to record the interaction and see which components take the most time.
- Test with CPU throttling in the browser's performance panel. A fast development machine hides problems that users on slower devices will see.
- Profile a production build. Development builds are noticeably slower, and they can exaggerate the issue.
If the profiler shows a render of a few milliseconds, there is nothing to fix. If a single component takes most of the time, consider whether you can make that render cheaper first. Transitions are for cases where the work is real and cannot easily be avoided.
Exercises
Exercise
A page has a text field and a chart that takes 300 ms to render from the field's value. You hold the value in one useState and pass it to both. Typing feels slow. Name the urgent update and the non-urgent update, and explain why one shared state causes the lag.
Show answer
The urgent update is the text the field displays. The non-urgent update is the chart. With one shared state, every keystroke produces a single update that renders the field and the chart together, so the field cannot paint until the 300 ms chart render finishes. The fix is to keep one state for the field and a second state for the chart, and to set the second one inside a transition.
Exercise
In the split search example, you remove memo from SlowList. The code still runs and the list still updates, but typing is slow again. Why?
Show answer
When setText runs, App re-renders at urgent priority, and without memo it re-renders SlowList as well, even though query has not changed. The 200 ms of list rendering therefore happens inside the urgent render and blocks the field. With memo, React sees that SlowList received the same props and skips it, so only the transition render pays for the list.
Exercise
A user types "react" quickly while the list is still rendering the results for "rea". What does React do with the unfinished render, and which intermediate results does the user see?
Show answer
React discards the unfinished render for "rea" and restarts with the newest query. The user does not see results for the skipped intermediate values. The field updated for every character, but the list shows only results from renders that completed, which in a fast burst of typing may be just the final one.