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

Heavy Computation Example: Complex Visualizations

Explore how charts, graphs, or canvas rendering can monopolize the thread and benefit from useTransition.

Heavy Computation Example: Complex Visualizations

You'll learn how rendering charts, graphs, and canvas elements can freeze your UI and why useTransition is the perfect remedy.

The Problem: Visualizations That Block

Complex visualizations—think D3 charts, Canvas-based graphics, or SVG graphs with hundreds of data points—require heavy computation. Each time you update them (say, changing a filter or time range), React must:

  1. Calculate coordinates, scales, and transformations
  2. Render potentially thousands of DOM nodes or canvas operations
  3. Apply styles and animations

This work can easily exceed the 50ms threshold, making your entire UI feel frozen. If a user types in a filter input while a chart re-renders, their keystrokes lag or disappear entirely.

The Solution: Let Visualizations Wait

Wrap the state update that triggers the expensive chart in startTransition:

const [timeRange, setTimeRange] = useState('week');
const [isPending, startTransition] = useTransition();

function handleRangeChange(newRange) {
  startTransition(() => {
    setTimeRange(newRange); // Chart re-renders as low-priority
  });
}

Now the button click responds instantly, and the chart updates when the browser has spare cycles. Your UI stays responsive even if the chart takes 200ms to recalculate.

The Analogy

Imagine a cashier ringing up items while also restocking shelves. Without priorities, they'd finish restocking before acknowledging your "excuse me." With useTransition, they pause restocking, help you, then resume—exactly how React pauses chart rendering to handle your next click.

When This Matters Most

  • Canvas rendering with complex paths
  • Large SVG graphs (100+ nodes/edges)
  • Real-time data visualizations
  • 3D renderings with WebGL
  • Heat maps or geographic visualizations

Key Takeaway: Charts, graphs, and canvas operations often monopolize the main thread for 100ms+, making them prime candidates for useTransition to keep your UI responsive during visual updates.