Sub topic 4
Marking updates as transitions with startTransition
This chapter assumes you know useState, how a state update triggers a re-render, and the difference between urgent and non-urgent updates from the earlier chapters. It also assumes your app renders through createRoot.
What startTransition does
startTransition is a function exported by react. You pass it a callback. Every state update that the callback schedules is marked as a transition, which is a non-urgent update. React renders a transition in the background and can interrupt that render if something more urgent arrives, such as a click or a keystroke.
import { useState, startTransition } from 'react';
function Tabs({ tabs }) {
const [tab, setTab] = useState(tabs[0]);
function selectTab(nextTab) {
startTransition(() => {
setTab(nextTab);
});
}
return (
<div>
{tabs.map((name) => (
<button key={name} onClick={() => selectTab(name)}>
{name}
</button>
))}
<TabContent name={tab} />
</div>
);
}TabContent stands for any slow component that you provide. Without startTransition, clicking a tab blocks the page until TabContent finishes rendering. With it, React keeps the old tab on screen and stays responsive. If you click a third tab while the second is still rendering, React drops the unfinished render and starts on the newest one.
What happens during a transition
The screen never shows tab B here. React only commits a transition when its render completes, and the old UI stays interactive until then.
useTransition versus startTransition
useTransition returns a startTransition function and an isPending flag. The standalone startTransition has no isPending. The two differ in where you can call them.
| Row | `useTransition` | standalone `startTransition` |
|---|---|---|
| Where it can be called | Inside a component or custom Hook | Anywhere, including outside components |
| Pending flag | `isPending` | None |
| Typical use | UI that needs a pending indicator | Libraries, stores, code with no Hook access |
Use the standalone form when you have no Hook available, or when you do not need a pending indicator. The next chapter covers useTransition and isPending.
import { startTransition } from 'react';
export function navigate(setRoute, nextRoute) {
startTransition(() => {
setRoute(nextRoute);
});
}This helper is plain JavaScript. A router could call it with a setRoute function that it got from useState.
Rules for the callback
React marks an update as a transition only if the setter runs while the callback is executing. Keep these rules in mind:
- The callback runs immediately and synchronously. React does not delay it.
- Only state updates are affected. Other work in the callback, such as logging or computing values, runs as usual.
- Setters called later are not marked. A setter inside
setTimeoutruns after the callback has returned, so it is an ordinary urgent update.
startTransition(() => {
// Not a transition: this runs after startTransition has returned.
setTimeout(() => setTab(nextTab), 1000);
});
setTimeout(() => {
// Correct: the setter runs inside the callback.
startTransition(() => setTab(nextTab));
}, 1000);Another limit is that startTransition cannot track updates made after an await in older React versions. React 19 accepts an async callback, but setters that run after an await still need their own startTransition wrapper. A later chapter covers this.
When it helps
A transition does not make rendering faster. It changes the priority of the render. It helps when an update triggers expensive rendering and you would rather keep the current UI usable than freeze it. If the render is already fast, a transition adds nothing.
Exercise
A click handler calls startTransition(() => { fetchData().then((d) => setData(d)); }). Why is setData not treated as a transition, and how do you fix it?
Show answer
The .then callback runs after startTransition has returned, so React is no longer inside the transition scope and treats setData as an urgent update. Wrap the setter itself: fetchData().then((d) => { startTransition(() => setData(d)); });.