Concurrent Rendering with useTransition
Concurrent Rendering with useTransition
Prior to React 18, state updates in React were synchronous and un-interruptible. If a state change triggered a computationally intensive re-render of thousands of DOM elements or complex charts, the main JavaScript thread froze, dropping frames and causing button clicks, text input typing, and scroll events to lag. useTransition unlocks Concurrent React, enabling you to mark state updates as non-blocking transitions.
1. Urgent Updates vs Non-Urgent Transitions
React divides user interactions into two core categories:
- 1Urgent Updates: Direct physical interactions requiring immediate feedback (typing in an input field, clicking a toggle button, hovering a dropdown). Users feel lag if these take longer than ~16ms.
- 2Transition Updates: Secondary UI views transitioning between states (filtering a table of 10,000 items, switching a search results tab, rendering complex data graphs). Users tolerate a brief delay as long as the page remains interactive.
2. Anatomy of useTransition
The useTransition hook returns a boolean flag isPending and a startTransition dispatcher function:
startTransition(callback): Wraps state setter calls. React marks the resulting re-render as low priority and interruptible. If an urgent update occurs while the transition is calculating, React pauses the transition, renders the urgent update, and resumes or restarts the transition.isPending: Boolean indicating whether a low-priority transition is currently running in the background.
3. High-Performance Filtering Example
Here is how useTransition keeps an input responsive while filtering 20,000 records:
4. Key Rules and Caveats
- 1Synchronous Setters Only: The callback passed to
startTransitionmust be synchronous. You cannot pass anasyncfunction or wrapsetTimeout:
- 1Cannot Control Text Input State Directly: Do not wrap controlled text inputs' immediate state in
startTransition; controlled inputs require urgent synchronous feedback to prevent cursor position jumping.
Practice Quiz
Deferred Value Updates with useDeferredValue
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Analyzing Bundle Sizes with Rollup and Webpack Visualizer | Deferred Value Updates with useDeferredValue |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.