Course contentsShow
← React
Lesson 2018 of 2,39645. AnimationsFree lesson

Triggering Layout Thrashing

Identify how reading layout properties (offsetWidth, etc.) during animation loops causes forced reflows.

Triggering Layout Thrashing

What you'll learn: How reading layout properties inside animation loops forces the browser to recalculate styles and layout on every frame, killing performance.

The Forced Reflow Problem

You've learned that transform and opacity are compositor-only properties—they animate smoothly because the browser doesn't need to recalculate layout. But if you read certain DOM properties during an animation loop, you force the browser to stop everything and compute layout immediately.

Properties like offsetWidth, offsetHeight, scrollTop, getBoundingClientRect(), and others require the browser to have up-to-date layout information. If you've made changes but haven't let the browser render yet, reading these properties triggers a forced synchronous layout (also called a "reflow").

Why It Thrashes

The deadly pattern looks like this:

  1. You change a style (write)
  2. You immediately read a layout property (read)
  3. Browser must recalculate layout right now
  4. Repeat 60 times per second

This write-read-write-read cycle inside a requestAnimationFrame loop or React state updates causes layout thrashing—the browser recalculates layout over and over instead of batching efficiently.

The Hidden Trap in React

Even innocent-looking code can trigger this. If your animation logic reads element.offsetWidth to calculate the next position, then updates state that changes styles, you've created the thrashing cycle. The browser can't optimize because you keep forcing it to calculate layout before it's ready to paint.

Key Takeaway: Avoid reading layout properties (offsetWidth, scrollTop, getBoundingClientRect()) inside animation loops or repeated React updates—batch reads separately from writes, or stick to compositor-only properties that don't need layout information at all.