Back to Blog
Lesson 11 of the Advanced React: Performance, Architecture & Patterns course
ReactJune 27, 20264 min read

Non-blocking UI with useTransition in React

Master useTransition to keep your React UI responsive. Learn how to mark state updates as non-urgent to prioritize user input during heavy rendering.

ReactPerformanceConcurrent ModeUI ResponsivenessHooksjavascriptfrontend

Previously in this course, we explored Introduction to Concurrent React: Time-Slicing and Performance, which established how React's concurrent engine can pause and resume rendering work. In this lesson, we build on those fundamentals to exert explicit control over that process using useTransition.

When building complex dashboards or data-heavy interfaces, a single user action—like typing in a search bar—often triggers a massive re-render of a list or chart. If that render takes 200ms, the input field feels "stuck." useTransition is your primary tool to tell React, "This specific update is less important than the user typing; let the input stay responsive while you process the heavy update in the background."

Understanding Transitions from First Principles

In standard React, all state updates are considered "urgent." When you call setState, React enters a synchronous render phase to update the DOM. If the component tree is deep or the data transformation is complex, the main thread remains blocked until the work completes.

A transition is a specific category of state update that React treats as non-urgent. By wrapping a state setter in startTransition, you instruct React to:

  1. Interrupt: If a more urgent update (like a keystroke) comes in, pause the ongoing transition work.
  2. Defer: Keep the current UI interactive while the "transitioning" state is computed in the background.
  3. Commit: Once the background work is finished, apply the changes to the DOM.

Worked Example: Filtering Large Lists

Imagine a dashboard displaying a list of 5,000 items. Filtering this list on every keystroke usually creates noticeable input lag. Let's optimize this using useTransition.

TSX
import { useState, useTransition } from CE9178">'react';

function LargeListFilter({ items }) {
  const [query, setQuery] = useState(CE9178">'');
  const [filteredItems, setFilteredItems] = useState(items);
  const [isPending, startTransition] = useTransition();

  const handleInputChange = (e) => {
    const value = e.target.value;
    
    // 1. Urgent: Update the input field immediately
    setQuery(value);

    // 2. Non-urgent: Filter the list in the background
    startTransition(() => {
      const nextFiltered = items.filter(item => 
        item.name.toLowerCase().includes(value.toLowerCase())
      );
      setFilteredItems(nextFiltered);
    });
  };

  return (
    <div>
      <input 
        value={query} 
        onChange={handleInputChange} 
        placeholder="Filter items..." 
      />
      {isPending && <p>Updating list...</p>}
      <ul style={{ opacity: isPending ? 0.5 : 1 }}>
        {filteredItems.map(item => <li key={item.id}>{item.name}</li>)}
      </ul>
    </div>
  );
}

In this example, the setQuery call happens immediately, ensuring the input value stays in sync with the user's keystrokes. The setFilteredItems call is wrapped in startTransition, allowing React to prioritize the input update and process the list filter as a low-priority task. The isPending boolean provides a hook to give visual feedback, such as dimming the list or showing a spinner.

Managing Pending States

While isPending is useful, rely on it sparingly. Over-using loading indicators can lead to "UI flickering" if the transition finishes too quickly.

FeatureUrgent State (setState)Transition State (startTransition)
PriorityHigh (Immediate)Low (Background)
User InputBlocked until finishInterrupted by input
Use CaseTyping, Toggling, SelectingFiltering, Sorting, Data Views

Hands-on Exercise

Refactor an existing search component in your project:

  1. Introduce useTransition to your search input handler.
  2. Observe the render count in the React DevTools Profiler (as taught in Profiling with React DevTools: Identifying Performance Bottlenecks).
  3. Add a visual indicator (like a CSS class or isPending flag) to signify that the list is stale while the transition is running.
  4. Compare the "Interaction to Next Paint" (INP) score before and after this change, as discussed in INP Optimization: Architecting Non-Blocking DOM Updates.

Common Pitfalls

  • Wrapping Everything: Don't wrap every setState in startTransition. If you wrap something that needs to be immediate (like a checkbox toggle or a button click), the UI will feel sluggish and unresponsive.
  • Missing Dependencies: Since startTransition is a function, ensure that any state derived inside it is correctly handled. If you use useMemo for the heavy calculation inside the transition, the state update won't actually be deferred until the calculation is done. Always perform the state-setting action inside the transition callback.
  • Async/Await: You cannot use await inside the startTransition callback. Transitions are for synchronous state updates. If you need to handle asynchronous data fetching, look toward useDeferredValue or Suspense, which we will cover in the next few lessons.

Recap

useTransition is the cornerstone of keeping your React application responsive during heavy data operations. By distinguishing between urgent user interactions and non-urgent data processing, you create a fluid experience that feels significantly faster to the end user. Remember: only defer updates that are visually heavy or computationally expensive.

Up next: We will explore how to synchronize deferred UI states using useDeferredValue.

Similar Posts