Sub topic 1
Urgent and non-urgent updates
React 18 and later can treat some state updates as more important than others. This chapter explains the two kinds of updates, why the difference matters, and what goes wrong when React treats every update the same way. Later chapters cover the APIs that mark updates as non-urgent.
Prerequisites
This chapter assumes you have the following:
- You can write React function components and JSX.
- You know
useStateand how props flow down a component tree. - You know that a state change makes React re-render a component and its children.
Knowing React.memo and Suspense helps, but you don't need them yet.
Two kinds of updates
Not every state change needs an instant response. React distinguishes two categories:
- Urgent updates reflect direct interaction: typing, clicking, pressing a key. The user expects the screen to respond immediately. If it doesn't, the interface feels broken.
- Non-urgent updates (also called transition updates) move the UI from one view to another. Examples are showing search results, switching tabs, or navigating to a new page. Users accept a short delay here, as long as the interface stays usable while they wait.
A single user action often triggers both. Typing in a search box must update the input at once (urgent). The filtered list below it can arrive a moment later (non-urgent).
The problem: every update is urgent by default
Without any hints, React treats every setState call as urgent. Once React starts rendering, it finishes the work before the browser can handle new input. If rendering is slow, the page freezes.
This example makes the problem visible. Each SlowItem blocks for about one millisecond, so 200 items cost roughly 200 ms per render:
import { useState } from 'react';
function SlowItem({ text }) {
const start = performance.now();
while (performance.now() - start < 1) {
// Busy-wait to simulate an expensive component.
}
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>
</>
);
}Type quickly in this input and the characters appear late or in bursts. The input is tied to the same state as the slow list, so every keystroke waits for all 200 items to render.
What changes with concurrent rendering
The fix is not to make the work disappear. It is to let React do the urgent part first and the rest later, in a way that can be interrupted. The flow looks like this:
Three rules follow from this model:
- Urgent updates can interrupt non-urgent renders that are in progress.
- An interrupted render is thrown away. React starts over with the latest state, so the screen never shows a half-finished result.
- A non-urgent update that never finishes before the next keystroke is simply skipped, which also avoids wasted work.
This ability to pause and restart rendering is called concurrent rendering. It changes how React schedules work. It does not run your components in parallel on multiple threads.
What you decide and what React decides
React cannot guess which updates are non-urgent. You mark them. The tools you will meet in the next chapters are:
| Tool | What you mark |
|---|---|
| `startTransition` / `useTransition` | A state update that can wait |
| `useDeferredValue` | A value that may lag behind its source |
Everything you do not mark stays urgent. This is a safe default, because the first rule of the model is that direct interaction is never delayed.
Exercise
A user types into a search box. The input shows the typed text, and a long result list is filtered by that text. Which update should be urgent, and which should be non-urgent? What happens to an in-progress list render when another keystroke arrives?
Show answer
Updating the input's value is urgent, because the user must see their typing at once. Filtering the result list is non-urgent and may lag. If another keystroke arrives, React discards the in-progress list render and restarts it with the newest text, so only a complete, current result is ever shown.