Marking State Updates as Non-Urgent
What you'll learn: How to use startTransition to tell React which state updates can wait, keeping your UI responsive during heavy rendering.
The Problem: Not All Updates Are Equally Important
When you type in a search box, two things usually need to happen: the input value updates (urgent) and the search results re-render (potentially slow). If both happen synchronously, typing feels sluggish because React blocks the next keystroke until all rendering finishes.
How startTransition Prioritizes Updates
By wrapping a setState call in startTransition, you're telling React: "This update is less urgent—if something more important comes along (like another keystroke), pause this and handle that first."
function SearchPage() {
const [query, setQuery] = useState('');
const [results, setResults] = useState([]);
const [isPending, startTransition] = useTransition();
function handleChange(e) {
// Urgent: update the input immediately
setQuery(e.target.value);
// Non-urgent: filter results can wait
startTransition(() => {
const filtered = hugeList.filter(item =>
item.includes(e.target.value)
);
setResults(filtered);
});
}
return <input value={query} onChange={handleChange} />;
}
The setQuery outside startTransition happens immediately—your input stays responsive. The setResults inside startTransition can be interrupted if the user keeps typing, preventing UI lag.
Think of It Like Triage
Imagine a hospital emergency room: a patient with a scraped knee (non-urgent update) should wait if someone arrives with a heart attack (urgent update). React's scheduler works the same way—urgent updates jump the queue.
Key Takeaway: Wrapping setState in startTransition marks that update as interruptible and lower-priority, allowing React to keep the UI responsive by prioritizing urgent updates like user input over slower rendering work.