Sub topic 5
useTransition and the isPending flag
This chapter assumes you know React function components, useState, props, and how a state change causes a re-render. You should also know that React 18 or later is required, and that your app renders through createRoot.
What useTransition gives you
startTransition marks a state update as non-urgent. The useTransition hook does the same job and also tells you whether that update is still being worked on. You call it inside a component:
const [isPending, startTransition] = useTransition();It returns an array of two items:
isPendingis a boolean. It istruefrom the moment you start a transition until React has committed the result to the screen.startTransitionis a function. You pass it a callback, and every state update made synchronously inside that callback is treated as a transition.
Updates outside the callback stay urgent. React handles urgent updates first. It can pause or throw away the render of a transition if something more urgent arrives, such as a click or a keystroke.
Worked example: tabs with a slow panel
Suppose one tab renders a large, slow component. Without a transition, clicking that tab freezes the page until the render finishes. With a transition, the page keeps responding.
import { useState, useTransition } from 'react';
function Tabs({ tabs }) {
const [tab, setTab] = useState(tabs[0]);
const [isPending, startTransition] = useTransition();
function selectTab(nextTab) {
startTransition(() => {
setTab(nextTab);
});
}
return (
<div>
{tabs.map((name) => (
<button key={name} onClick={() => selectTab(name)}>
{name}
</button>
))}
<div style={{ opacity: isPending ? 0.6 : 1 }}>
<TabContent name={tab} />
</div>
</div>
);
}Here is what happens when the user clicks a new tab:
- React sets
isPendingtotrueand renders that small change right away. The old content dims. - React starts rendering
TabContentwith the newtabvalue in the background. - If the user clicks another tab during this render, React abandons the unfinished work and starts over with the newer value.
- When the render completes, React commits it and
isPendingbecomesfalse.
The old tab stays on screen and clickable the whole time. The user never sees a half-built panel.
Where the pending flag fits
isPending is ordinary state that React manages for you. Use it for feedback that must appear immediately, such as dimming content, showing a small spinner, or disabling a button.
Keeping an input responsive
A common use is to split one input into an urgent value and a slow value. The input shows what the user typed. The expensive list reads a second state variable that updates in a transition.
import { useState, useTransition } from 'react';
function Search({ items }) {
const [text, setText] = useState('');
const [filter, setFilter] = useState('');
const [isPending, startTransition] = useTransition();
function handleChange(e) {
setText(e.target.value);
startTransition(() => {
setFilter(e.target.value);
});
}
return (
<>
<input value={text} onChange={handleChange} />
{isPending && <p>Updating...</p>}
<ResultList items={items} filter={filter} />
</>
);
}setText runs outside the transition, so the input always reflects the latest keystroke. Only setFilter is deferred.
Common errors
- Using a transition for the wrong update. Wrapping the
setTextcall above makes typing feel broken. Wrap only the update that causes the expensive render. - Expecting it to make rendering faster. A transition does not speed up a slow component. It only lets React interrupt it so other work can proceed.
- Updates after an
await. In React 18, only updates made synchronously inside the callback count as transitions. AsetStatethat runs after anawaitis a normal urgent update. React 19 lets the callback beasync, andisPendingstaystrueuntil it settles, but updates after anawaitstill need their ownstartTransition:
import { useState, useTransition } from 'react';
function NameForm() {
const [name, setName] = useState('');
const [error, setError] = useState(null);
const [isPending, startTransition] = useTransition();
function handleSubmit() {
startTransition(async () => {
const result = await updateName(name);
startTransition(() => {
setError(result.error ?? null);
});
});
}
return (
<>
<input value={name} onChange={(e) => setName(e.target.value)} />
<button onClick={handleSubmit} disabled={isPending}>
Save
</button>
{error && <p>{error}</p>}
</>
);
}In this example updateName stands for any async function you provide.
Exercise
In the Search component, a teammate moves setText(e.target.value) inside the startTransition callback. What problem will users notice, and why?
Show answer
The input will lag or feel stuck while the list renders. Updates inside a transition are non-urgent, so React may postpone the new text value, and the controlled input does not show the latest keystroke right away. Keep setText outside the transition and wrap only setFilter.
Exercise
A click handler calls startTransition(async () => { await save(); setSaved(true); }) in React 19. Why might setSaved(true) not behave as a transition, and how do you fix it?
Show answer
State updates made after an await lose the transition context, so setSaved(true) runs as an urgent update. Wrap it in another startTransition(() => { setSaved(true); }) call after the await.