Sub topic 10
Limitations and Common Pitfalls
This chapter assumes you have used useTransition, startTransition and useDeferredValue from the earlier chapters. It also assumes you know what memo does and that your app renders through createRoot (React 18 or later). Each section below covers one way these tools fail, why it happens, and how to fix it.
What transitions do not do
Transitions and deferred values do not make rendering faster. They change when React renders and whether React can stop partway through. The total CPU work is the same, and often slightly higher because React may render a tree, discard it, and render again.
Three limits follow from this:
- React can only interrupt a render between components. A single component that takes 300 ms to render blocks the main thread for those 300 ms, even inside a transition.
- The commit phase is not interruptible. Once React starts applying DOM changes, it finishes. Effects run after that.
- Interruption needs a concurrent root. With the legacy
ReactDOM.render,startTransitionstill runs, but rendering is not interruptible and you get no benefit.
Pitfall: wrapping a controlled input in a transition
A controlled input must update synchronously, because the browser needs the new value in the same event. If you put setText inside a transition, React treats the update as non-urgent and may render the old value first. The visible result is an input that lags, drops characters or jumps back.
import { useState, useTransition } from 'react';
function BrokenSearch() {
const [text, setText] = useState('');
const [, startTransition] = useTransition();
function handleChange(e) {
const value = e.target.value;
startTransition(() => {
setText(value); // Wrong: the input value is now a non-urgent update
});
}
return <input value={text} onChange={handleChange} />;
}The fix is to split the state. Keep one urgent state for the input and one transition state for the expensive consumer, as in the Search example from earlier in this tutorial. If you cannot split the state, use useDeferredValue on the value the expensive part reads.
Pitfall: updates that escape the synchronous callback
React marks an update as a transition only if setState is called synchronously while the callback passed to startTransition is running. The callback itself runs immediately, not later. Any update scheduled from inside the callback to run later is urgent.
Timers
import { useState, startTransition } from 'react';
function Pager() {
const [page, setPage] = useState('home');
function wrong() {
startTransition(() => {
// The timer callback runs later, outside the transition scope
setTimeout(() => setPage('about'), 1000);
});
}
function right() {
setTimeout(() => {
startTransition(() => setPage('about'));
}, 1000);
}
return (
<>
<button onClick={wrong}>Wrong</button>
<button onClick={right}>Right</button>
<p>Page: {page}</p>
</>
);
}The rule is to put startTransition closest to the setState call, not around the code that eventually triggers it.
After an await
In React 18, startTransition does not accept an async function. In React 19, an async function works and isPending stays true until the promise settles. However, React only tracks the synchronous part up to the first await. A setState after an await is an ordinary urgent update unless you wrap it in a new startTransition.
import { useState, useTransition } from 'react';
function Saver({ save }) {
const [message, setMessage] = useState('');
const [isPending, startTransition] = useTransition();
function handleClick() {
startTransition(async () => {
const result = await save();
// Without this inner startTransition, setMessage is urgent
startTransition(() => {
setMessage(result);
});
});
}
return (
<>
<button onClick={handleClick} disabled={isPending}>Save</button>
<p>{message}</p>
</>
);
}Here save is any async function you pass in as a prop.
Pitfall: several transitions batch together
If a second transition starts while the first is still rendering, React currently combines them. It does not finish them independently. In practice, two transitions that update different state (for example, two tabs in unrelated panels) can delay each other's results. The React documentation calls this a limitation that may be removed later. Do not design a UI that relies on two transitions completing independently.
An interrupted transition is also restarted from scratch, not resumed. If the user clicks three tabs quickly, React drops the work for the first two and renders only the last.
Pitfall: deferring a value that changes identity every render
useDeferredValue compares the new value with the previous one using Object.is. A primitive such as a string is fine. An object or array created during render is a new value on every render, so React sees a change each time and schedules an endless series of background re-renders.
import { useDeferredValue, useMemo } from 'react';
function Results({ query, sort }) {
// Wrong: `options` is a new object on every render
// const options = { query, sort };
// const deferred = useDeferredValue(options);
// Right: defer primitives, then build the object from them
const deferredQuery = useDeferredValue(query);
const options = useMemo(
() => ({ query: deferredQuery, sort }),
[deferredQuery, sort]
);
return <pre>{JSON.stringify(options)}</pre>;
}Pass primitives, or objects created outside of render, to useDeferredValue.
Pitfall: expecting a delay or a network saving
useDeferredValue is not a timer. It has no fixed delay. React renders the old value first and starts a background render with the new one immediately. On a fast device the deferred value may catch up so quickly that you never see the stale state.
It also does not reduce the number of network requests. If an effect fires a fetch whenever query changes, the fetch still fires on every keystroke. Use the deferred value as the effect dependency if you want fewer fetches. The effect then runs only when the deferred value commits. Debouncing is the right tool when the goal is to reduce requests.
Pitfall: the deferred child is not memoized
If the expensive child is not wrapped in memo, it re-renders on every urgent update with its props, so the deferral saves nothing. The urgent render reaches the child, and the child does the slow work again. Memoize the child so that React can skip it when the deferred value has not changed yet.
Pitfall: render functions that are not pure
Concurrent rendering may start rendering a component, throw the result away, and render it again. React may also call render functions twice in Strict Mode during development. This breaks components that:
- mutate a variable outside the component during render,
- read mutable data from outside React, such as a global store or
windowproperties, and display it, - send requests or write to storage inside render.
Reading a mutable store during an interruptible render can produce tearing, where one part of the screen shows the old value and another shows the new value. For external stores, use useSyncExternalStore. It forces a synchronous render for updates, which avoids tearing. Keep side effects in event handlers and effects.
Pitfall: expecting isPending where you did not call the hook
isPending is only returned by the useTransition hook, and it belongs to the component that called it. The standalone startTransition imported from react has no pending flag. If you need to show a spinner, call useTransition in a component that renders the indicator, or pass isPending down as a prop.
Also remember that isPending is true only while the transition is rendering. It does not cover work that happens outside React, such as a server round trip started in an event handler without an async transition.
Symptoms and likely causes
| Symptom | Likely cause | Fix |
|---|---|---|
| Typing in an input lags or drops characters | The input's state is set inside `startTransition` | Use a separate urgent state for the input |
| Spinner never appears, UI freezes anyway | One component is slow, or the root is legacy | Optimize that component, and use `createRoot` |
| Update after `await` is urgent | `setState` was not wrapped again | Wrap it in a nested `startTransition` |
| Background renders keep repeating | An object is passed to `useDeferredValue` | Defer primitives and memoize the derived object |
| Deferred list still re-renders on every keystroke | The child is not wrapped in `memo` | Memoize the child |
| Two transitions finish together | React batches transitions | Do not depend on independent completion |
A quick diagnosis path
Summary of rules
- Never put a controlled input's own state in a transition.
- Call
setStatesynchronously insidestartTransition, and wrap each update after anawaitagain. - Pass primitives to
useDeferredValue, and memoize the child that receives them. - Keep render pure and use
useSyncExternalStorefor external data. - Treat transitions as a way to schedule work, not to remove it.
Exercise
A developer writes startTransition(async () => { const data = await fetchResults(text); setResults(data); }) in React 19. The spinner driven by isPending works, but the results appear as an urgent update and sometimes replace content that is already showing. What is wrong, and how do you fix it?
Show answer
The setResults(data) call happens after an await, so React no longer treats it as part of the transition. Wrap that call in a nested transition: startTransition(() => { setResults(data); }) inside the async function. isPending stays true until the whole async function finishes, but only the nested update is marked as a transition.
Exercise
A component calls const deferred = useDeferredValue({ query, page }) and passes deferred to a memoized child. The child re-renders in the background after every render of the parent, even when nothing changed. Why, and what is the fix?
Show answer
The object literal is a new object on every render, so Object.is never finds the deferred value equal to the previous one, and React keeps scheduling background renders. Defer the primitive query with useDeferredValue(query) instead, then build the object from the deferred query and page with useMemo.