Skip to main content

Lesson 7 of 11

useLayoutEffect & useInsertionEffect

useLayoutEffect fires synchronously after DOM mutations but BEFORE the browser paints - essential for DOM measurements (element size/position) to avoid visual flicker. useInsertionEffect fires before DOM mutations - used exclusively by CSS-in-JS libraries to inject styles without layout thrashing.

20 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

useLayoutEffect - Measuring Before Paint

useLayoutEffect - Measuring Before Paint
import { useLayoutEffect, useRef, useState } from 'react';

// useEffect fires AFTER paint - reading layout causes a visible flash
// useLayoutEffect fires BEFORE paint - reading layout is seamless

// Example: tooltip positioned relative to a target element
function Tooltip({ targetRef, text }: { targetRef: React.RefObject<HTMLElement>; text: string }) {
  const tooltipRef = useRef<HTMLDivElement>(null);
  const [position, setPosition] = useState({ top: 0, left: 0 });

  useLayoutEffect(() => {
    if (!targetRef.current || !tooltipRef.current) return;
    const targetRect = targetRef.current.getBoundingClientRect();
    const tooltipRect = tooltipRef.current.getBoundingClientRect();

    setPosition({
      top: targetRect.top - tooltipRect.height - 8,
      left: targetRect.left + (targetRect.width - tooltipRect.width) / 2,
    });
    // Runs synchronously โ†’ position is set before paint โ†’ no flicker
  });

  return (
    <div
      ref={tooltipRef}
      style={{ position: 'fixed', top: position.top, left: position.left }}
    >
      {text}
    </div>
  );
}

// Rule of thumb:
// useEffect     โ†’ for most side effects (API calls, subscriptions, timers)
// useLayoutEffect โ†’ for DOM measurements, preventing visual flicker
//                   (and only on the client - skip in SSR with a guard)

// SSR guard - useLayoutEffect causes a warning in Next.js without this
const useIsomorphicLayoutEffect = typeof window !== 'undefined'
  ? useLayoutEffect
  : useEffect;

Common Mistakes - useLayoutEffect

  • Using useLayoutEffect when useEffect is sufficient - since useLayoutEffect blocks painting, overusing it can degrade performance; only use it when you specifically need post-DOM but pre-paint timing
  • Not guarding against SSR - Next.js renders on the server where `window` doesn't exist; useLayoutEffect causes a warning; use the isomorphicLayoutEffect pattern or conditional import
  • Heavy computation in useLayoutEffect - since it blocks the paint, any slow synchronous code causes visible jank; keep it fast and focused on DOM measurements only

Tip

Tip

Practice useLayoutEffect useInsertionEffect in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagramโ€ฆ

Visual architecture and flow for useLayoutEffect & useInsertionEffect

Practice Task

Note

Practice Task - (1) Write a working example of useLayoutEffect useInsertionEffect from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with useLayoutEffect useInsertionEffect is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.

Key Takeaways

  • useLayoutEffect fires synchronously after DOM mutations but BEFORE the browser paints - essential for DOM measurements (element size/position) to avoid visual flicker.
  • Using useLayoutEffect when useEffect is sufficient - since useLayoutEffect blocks painting, overusing it can degrade performance; only use it when you specifically need post-DOM but pre-paint timing
  • Not guarding against SSR - Next.js renders on the server where `window` doesn't exist; useLayoutEffect causes a warning; use the isomorphicLayoutEffect pattern or conditional import
  • Heavy computation in useLayoutEffect - since it blocks the paint, any slow synchronous code causes visible jank; keep it fast and focused on DOM measurements only

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/11 done