Sub topic 6
Showing pending and stale content
This chapter assumes you know useState, props, and how a React component re-renders. You should also have seen useTransition and useDeferredValue in the previous chapters.
A transition or a deferred value keeps the old screen visible while React renders the new one in the background. For a moment the user sees content that is out of date. Your job is to tell them so, without making the page jump or flash.
Two sources of "pending"
React gives you a different signal for each tool.
| Tool | Signal | Meaning |
|---|---|---|
| `useTransition` | `isPending` | A transition has started and has not committed yet. |
| `useDeferredValue` | `value !== deferredValue` | The deferred value still lags behind the urgent one. |
Both signals are ordinary values you can use during render. Neither one needs extra state.
Dimming content during a transition
isPending becomes true right after startTransition is called. It stays true until React commits the transition's render. The old content stays on screen during that time, so a light visual change is usually enough.
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
aria-busy={isPending}
style={{
opacity: isPending ? 0.6 : 1,
transition: 'opacity 0.2s 0.2s',
}}
>
<TabContent name={tab} />
</div>
</div>
);
}Two details matter here:
- The CSS
transitionhas a 0.2 second delay. A fast render finishes before the dimming starts, so the user never sees a flicker. aria-busytells assistive technology that the region is updating. Opacity alone is invisible to a screen reader.
Marking deferred content as stale
useDeferredValue has no isPending. You compare the two values instead.
import { useState, useDeferredValue, memo } from 'react';
const SlowList = memo(function SlowList({ query, items }) {
const visible = items.filter((item) => item.includes(query));
return (
<ul>
{visible.map((item) => (
<li key={item}>{item}</li>
))}
</ul>
);
});
function App({ items }) {
const [query, setQuery] = useState('');
const deferredQuery = useDeferredValue(query);
const isStale = query !== deferredQuery;
return (
<>
<input value={query} onChange={(e) => setQuery(e.target.value)} />
<div style={{ opacity: isStale ? 0.6 : 1 }}>
<SlowList query={deferredQuery} items={items} />
</div>
</>
);
}While you type, query updates first and deferredQuery follows later. In between, isStale is true and the list is dimmed. The input never dims, because it reads the urgent value.
Choosing the indicator
- Dim the old content when the layout stays the same. This is the cheapest option and causes no layout shift.
- Show a small inline message, such as
Updating..., when dimming is hard to see. Reserve space for it so the content below does not move. - Avoid replacing the content with a spinner. Hiding content that was already visible is the behavior transitions exist to prevent.
Common mistakes
- Reading
isPendingfrom the wrong hook. EachuseTransitioncall tracks only the transitions started through its ownstartTransition. The standalonestartTransitionimported fromreacthas no pending flag at all. - Putting the indicator inside the slow component. If the slow subtree is the one rendering, the indicator waits behind it. Render it in a parent or sibling that updates urgently.
- Using the pending flag to disable the input. Typing is meant to stay available. Disable only the actions that would conflict, such as a save button.
Exercise
A user types in a search box. The result list uses useDeferredValue(query). How do you detect that the list is out of date, and why should the input itself not be dimmed?
Show answer
Compare the urgent value with the deferred one: const isStale = query !== deferredQuery. Apply the dimming style only to the list. The input reads the urgent query, which is always current, so dimming it would suggest typing is blocked when it is not.