Course contentsShow
← React
Lesson 563 of 2,39615. useEffect FundamentalsFree lesson

Visual Flicker and Layout Effect Use Cases

Identifying scenarios where useLayoutEffect prevents visible flashes: tooltips, measurements, animations.

Visual Flicker and Layout Effect Use Cases

What you'll learn: When to reach for useLayoutEffect to prevent visible flashes that useEffect would cause.

The Visual Flicker Problem

Remember that useEffect runs after the browser paints the screen. This timing is usually perfect—but sometimes it creates a visible problem:

  1. Component renders with initial state
  2. Browser paints that initial state to the screen
  3. Your effect runs and updates state
  4. Component re-renders and paints again

The user sees both paints—a brief flicker of the "wrong" state before the correction appears.

When useLayoutEffect Prevents Flickers

useLayoutEffect runs synchronously after render but before paint, letting you measure or adjust the DOM before the user sees anything. This is critical for:

Tooltips and Popovers

You render a tooltip, measure if it fits on screen, then reposition it. Without layout effects, users would see it jump from the wrong position to the correct one.

DOM Measurements

Reading an element's size or position to calculate where other elements should go. The measurement must happen before paint to avoid visible recalculation.

Animations That Need Starting Values

Setting up animation start states based on current DOM layout. If you use useEffect, the element might briefly appear in its end state before animating.

The Analogy

Think of a photographer adjusting their subject's pose. useEffect is like fixing the pose after taking the photo—you'll need a reshoot. useLayoutEffect is fixing it before the shutter clicks—no one ever sees the mistake.

Key Takeaway: Use useLayoutEffect when your effect must read or modify layout to prevent visible flickers—tooltips, measurements, and layout-dependent animations are the classic cases.