Sub topic 3
Transitions, Deferred Values, and Suspense
This chapter assumes you know function components, useState, props, React.memo, and the basic purpose of Suspense. React 18 or later, rendered with createRoot, is required.
Transitions, deferred values, and Suspense
A transition marks a state update as non-urgent. React can pause its rendering when an urgent update arrives, such as another keystroke. If a transition causes a component to suspend, React keeps already visible content instead of immediately replacing it with a fallback.
A deferred value applies the same idea at a different boundary. The component that owns the value updates immediately, while a child receives the previous value until React finishes rendering the new one.
function Search({ items }) {
const [query, setQuery] = useState('');
const deferredQuery = useDeferredValue(query);
const isStale = query !== deferredQuery;
return (
<>
<input value={query} onChange={(e) => setQuery(e.target.value)} />
<Suspense fallback={<p>Loading results...</p>}>
<div style={{ opacity: isStale ? 0.6 : 1 }}>
<ResultList query={deferredQuery} items={items} />
</div>
</Suspense>
</>
);
}If ResultList suspends while loading data, the old results remain visible during the background render. ResultList should usually be memoized so an urgent parent render does not repeat expensive work unnecessarily.
Limitations and common pitfalls
Transitions do not make expensive JavaScript faster. They only let React schedule rendering around urgent work. Do not put the controlled input's own state inside a transition. Update the input urgently and transition the expensive derived state.
A transition also does not automatically debounce network requests. Code after an await needs special care in async transitions.
Choosing the tool
| Tool | Best use |
|---|---|
| Transition | A state update starts expensive React rendering |
| Deferred value | A child should consume a value at a lower priority |
| Debouncing | Delay events, searches, or network requests until input pauses |
Async transitions and `initialValue` in React 19
React 19 allows an async function in startTransition. isPending remains true while it runs. State updates after await should be wrapped in another transition.
startTransition(async () => {
const result = await saveName(name);
startTransition(() => setError(result.error ?? null));
});React 19 also supports useDeferredValue(value, initialValue). During the first render, React returns initialValue; later renders follow the normal deferred-value behavior. This is useful when the initial expensive result is unavailable or should be represented by a placeholder.
Exercise
Which technique should you choose when typing must stay responsive while a large result list re-renders?
Show answer
Update the input urgently and use a transition or deferred value for the result list. Use debouncing instead when the main goal is delaying requests or other event-driven work.