Reducing JavaScript Work During Animations
You'll learn: How to keep animations smooth by deferring heavy React state updates until after the animation completes.
Even when you animate compositor-only properties like transform and opacity, your animation can still stutter if JavaScript is busy recalculating the component tree. The browser must pause rendering to execute your React code—and if that code triggers expensive re-renders during an animation, you'll see dropped frames.
The Problem: Concurrent Work
Imagine animating a modal sliding in while simultaneously updating a complex form state or filtering a large list. React tries to do both at once, and your smooth 60fps animation becomes choppy because JavaScript blocks the main thread.
The Solution: Defer Updates
React 18 introduced useTransition and useDeferredValue specifically for this scenario. They tell React: "This state update is low-priority—yield to urgent work (like animations) first."
const [isPending, startTransition] = useTransition();
function handleExpensiveUpdate(newValue) {
// Animation runs first, state update happens after
startTransition(() => {
setComplexState(newValue);
});
}
Or defer a value itself:
const deferredQuery = useDeferredValue(searchQuery);
// Render with `deferredQuery` for heavy lists
This lets the animation finish its frames while React schedules the heavy work for idle time—like letting people exit the elevator before trying to enter.
When to Use This
Apply this pattern when you're animating and updating state that triggers expensive renders: large lists, complex forms, or heavy calculations. Don't use it for simple state changes that render quickly.
Key Takeaway: Use useTransition or useDeferredValue to mark state updates as low-priority during animations, ensuring smooth frames by deferring expensive JavaScript work until after the visual transition completes.