Course contentsShow
← React
Lesson 1434 of 2,39636. Concurrent ReactFree lesson

Common Pattern: Search Input with Heavy Filtering

Applying useTransition to filter large lists while keeping the input responsive during typing.

Common Pattern: Search Input with Heavy Filtering

You'll learn how to keep search inputs responsive when filtering large datasets by using useTransition to defer expensive filter operations.

The Problem

When you have a search input that filters a large list (think thousands of items), typing can feel sluggish. Every keystroke triggers a re-filter of the entire dataset, blocking the input field from updating smoothly. Users see lag between typing and the character appearing on screen.

The Solution: useTransition for Filtering

By wrapping your filter state update in startTransition, you tell React: "Update the input immediately (urgent), but the filtered list can wait (non-urgent)." This keeps typing buttery smooth while the heavy filtering happens in the background.

const [searchTerm, setSearchTerm] = useState('');
const [filteredItems, setFilteredItems] = useState(largeList);
const [isPending, startTransition] = useTransition();

function handleSearch(e) {
  const value = e.target.value;
  
  // Urgent: update input display immediately
  setSearchTerm(value);
  
  // Non-urgent: expensive filtering can wait
  startTransition(() => {
    const results = largeList.filter(item =>
      item.name.toLowerCase().includes(value.toLowerCase())
    );
    setFilteredItems(results);
  });
}

The User Experience

The input responds instantly to every keystroke. Meanwhile, isPending turns true while filtering runs, letting you show a subtle loading indicator on the results list (not the input itself). Users can keep typing without interruption.

Key Takeaway: Use useTransition to separate urgent UI updates (text input) from expensive derived state (filtered results), preventing heavy computations from blocking user interactions.