Understanding Transitions
Relevant source files
The following files were used as context for generating this wiki page:- packages/react/src/ReactStartTransition.js
- packages/react-reconciler/src/tests/ReactTransition-test.js
- packages/react-reconciler/src/tests/ReactTransitionTracing-test.js
- packages/react-reconciler/src/tests/ReactDefaultTransitionIndicator-test.js
- packages/react-reconciler/src/tests/ReactExpiration-test.js
- packages/react/src/tests/ReactStartTransition-test.js
- packages/react-dom/src/tests/ReactStartTransitionMultipleRenderers-test.js
Transitions are a core concurrency feature in React that allow developers to mark certain UI updates as non-urgent. This mechanism helps keep an application responsive by allowing urgent updates, like user input, to interrupt slower, non-critical updates. When a state update is wrapped in a transition, React understands that it can defer rendering the new state while working on more important tasks, preventing the UI from freezing.
This system is particularly powerful when dealing with asynchronous operations or heavy rendering tasks. Transitions integrate with Suspense to avoid showing unwanted loading states for content that is already visible, providing a smoother user experience by waiting for new content to be ready before transitioning to the new view. The useTransition hook provides a pending state (isPending) that developers can use to show loading indicators, giving users feedback that a background process is underway.
Core APIs
React provides two primary APIs for working with transitions: React.startTransition and the useTransition hook.
Sources: packages/react/src/ReactStartTransition.js, packages/react-reconciler/src/tests/ReactTransition-test.js
React.startTransition(scope)
This function wraps a state update, marking it as a transition. It can be used anywhere, including in class components or outside of components entirely.
| Parameter | Description |
|---|---|
scope | A function that contains one or more state updates (e.g., setState(...)). |
This diagram illustrates the basic flow of startTransition.
This flowchart shows the basic execution path when an update is wrapped in startTransition.
Sources: packages/react/src/ReactStartTransition.js:28-44
useTransition()
This hook is used within functional components to manage transition states. It returns an array with two values:
| Return Value | Description |
|---|---|
isPending (boolean) | A flag that is true while the transition is active. This is useful for showing loading indicators or disabling UI elements. |
startTransition (function) | A function to wrap state updates in a transition, identical in purpose to React.startTransition. |
The isPending flag becomes true when the transition starts and remains true until the new content is ready to be displayed, even if the update involves asynchronous data fetching that triggers a Suspense boundary.
Sources: packages/react-reconciler/src/tests/ReactTransition-test.js:201-213
Transitions and Concurrency
Transitions are fundamental to React's concurrent rendering model. They allow React to work on multiple versions of the UI simultaneously and prioritize rendering work.
Interruption
A key feature of transitions is that they can be interrupted by higher-priority updates, such as those triggered by user input. If a high-priority update is scheduled while a transition is rendering, React will pause the transition, render the high-priority update, and then resume or restart the transition afterward.
This sequence diagram shows a transition being interrupted by a normal-priority update.
This diagram shows how a high-priority update interrupts a transition, ensuring the UI remains responsive.
Sources: packages/react-reconciler/src/tests/ReactTransition-test.js:771-817, packages/react-reconciler/src/tests/ReactExpiration-test.js:313-360
Entanglement
When multiple transitions are initiated close together, they can become "entangled." This means that React will treat them as a single, larger transition. The isPending state will remain true, and the UI will not update until all entangled transitions have completed. This prevents the UI from showing intermediate states that might be jarring to the user.
For example, if a transition to page 'B' is started, and before it completes, another transition to page 'C' is started, React will wait for 'C' to be ready before updating the UI. It will not show page 'B' in the interim.
This diagram shows how Transition 2 entangles with Transition 1, preventing an intermediate state (B) from being rendered.
Sources: packages/react-reconciler/src/tests/ReactTransition-test.js:218-301
Default Transition Indicator
React includes a mechanism to provide a default loading indicator for transitions that don't have an explicit pending UI (e.g., via isPending or useOptimistic). This is configured at the root level.
const root = ReactNoop.createRoot({
onDefaultTransitionIndicator() {
Scheduler.log('start');
return () => {
Scheduler.log('stop');
};
},
});
The onDefaultTransitionIndicator callback is invoked when a transition starts and suspends. It returns a cleanup function that is called when the transition completes. This feature is not triggered if the application already provides a pending state via isPending, an optimistic update, or a synchronous mutation.
Sources: packages/react-reconciler/src/tests/ReactDefaultTransitionIndicator-test.js:34-54, packages/react-reconciler/src/tests/ReactDefaultTransitionIndicator-test.js:57-93
Transition Tracing
For debugging and performance monitoring, React provides an experimental API for tracing transitions. This is enabled by providing unstable_transitionCallbacks when creating a root.
Sources: packages/react-reconciler/src/tests/ReactTransitionTracing-test.js
Transition Callbacks
The following callbacks can be provided to track the lifecycle of a transition:
| Callback | Description |
|---|---|
onTransitionStart | Called when a transition with a name begins. |
onTransitionProgress | Called when a transition suspends or unsuspends on a Suspense boundary. It receives a list of pending suspense boundaries. |
onTransitionComplete | Called when the transition and all its nested suspense boundaries have completed. |
These callbacks are only invoked for transitions that have a name option provided.
startTransition(() => {
/* state update */
}, { name: 'my-transition' });
Sources: packages/react-reconciler/src/tests/ReactTransitionTracing-test.js:192-220
Tracing Markers
Within a named transition, you can use React.unstable_TracingMarker to trace specific parts of the component tree. This helps in pinpointing which parts of a transition are slow.
<React.unstable_TracingMarker name="my-marker">
<Suspense fallback={<Loading />}>
<MyComponent />
</Suspense>
</React.unstable_TracingMarker>
The lifecycle of these markers can be tracked with additional callbacks:
| Callback | Description |
|---|---|
onMarkerProgress | Called when a marker's content suspends or unsuspends. |
onMarkerComplete | Called when a marker and all its children have finished rendering. |
onMarkerIncomplete | Called if a marker is unmounted or its name changes before it completes, indicating an aborted render for that section. |
This tracing is a powerful tool for understanding the performance characteristics of complex, concurrent UI updates.
Sources: packages/react-reconciler/src/tests/ReactTransitionTracing-test.js:1011-1060, packages/react-reconciler/src/tests/ReactTransitionTracing-test.js:1543-1605
Advanced Topics
Expiration
Transitions are scheduled with a lower priority and have a longer expiration time. However, if a transition takes too long to complete due to being CPU-bound, React will eventually expire it and render it synchronously to prevent starvation. This ensures that even low-priority work eventually completes.
Sources: packages/react-reconciler/src/tests/ReactExpiration-test.js:115-133
Multiple Renderers
React.startTransition is designed to work across multiple React renderers (e.g., React DOM and React Noop) simultaneously. A single transition can coordinate updates in components managed by different renderers, and pending states will be correctly synchronized across them.
Sources: packages/react-dom/src/tests/ReactStartTransitionMultipleRenderers-test.js:93-138
Usage Warnings
React warns developers if a large number of updates are detected inside a single startTransition call. This often indicates misuse, such as with a subscription-based state management pattern. The warning suggests using React-provided hooks like useSyncExternalStore instead, as large, synchronous batches of updates can undermine the benefits of concurrent rendering.
Sources: packages/react/src/tests/ReactStartTransition-test.js:29-53