0Pricing
React Academy · Lesson

Avoiding Layout Thrash and Visual Flicker

Identify when a useEffect causes visible flicker and replace it with useLayoutEffect to fix it.

What Is Layout Thrash

Layout thrash occurs when JavaScript alternates between reading DOM properties and writing to the DOM in a loop. Each read after a write forces the browser to recalculate the layout synchronously to return an accurate value. This can turn what should be a single layout calculation into dozens, severely hurting performance.

How React Batch Updates Help

React batches all setState calls within event handlers and effects into a single re-render. This natural batching prevents the read-write-read pattern that causes thrash within a single render cycle. In React 18, batching extends to async callbacks and Promises as well, reducing layout thrash from concurrent state updates.

All lessons in this course

  1. The Rendering Phase and DOM Commit
  2. When useLayoutEffect Runs vs useEffect
  3. Measuring DOM Elements with useLayoutEffect
  4. Avoiding Layout Thrash and Visual Flicker
← Back to React Academy