What is interruptible rendering in React?
Hint
Think about what happens to a slow render when the user types — and which APIs mark an update as low priority.
Full Answer
Before React 18, a render ran start to finish without stopping; a big update could freeze input for hundreds of milliseconds. With concurrent rendering, React works in small units (fibers) and checks between them whether something more urgent is waiting. If it is, it pauses or throws away the in-progress render, handles the urgent update, then restarts.
Only updates marked as non-urgent are interruptible. You mark them with startTransition, useTransition or useDeferredValue:
function Search() {
const [query, setQuery] = useState('');
const [results, setResults] = useState([]);
const [isPending, startTransition] = useTransition();
function onChange(e) {
setQuery(e.target.value); // urgent: input stays responsive
startTransition(() => {
setResults(filterHugeList(e.target.value)); // interruptible
});
}
return (
<>
<input value={query} onChange={onChange} />
{isPending ? <Spinner /> : <List items={results} />}
</>
);
}
The render phase is interruptible because it has no side effects. The commit phase, where React writes to the DOM, always runs synchronously, so users never see half an update.
More React Fundamentals Questions
Practice this in a timed sprint
5 free questions, no signup required