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

SSR Warnings with useLayoutEffect

Why useLayoutEffect causes warnings during server rendering and the useIsomorphicLayoutEffect pattern.

SSR Warnings with useLayoutEffect

You'll learn why useLayoutEffect triggers warnings during server-side rendering and how to handle it safely.

The Problem: Server Has No DOM

When React renders your component on the server (SSR/Next.js/Remix), there's a fundamental mismatch: useLayoutEffect expects to interact with the DOM synchronously before paint, but the server has no DOM at all. There's nothing to measure, no layout to read.

React can't run layout effects on the server, so it simply skips them. But it warns you:

Warning: useLayoutEffect does nothing on the server...

This warning exists because skipping the effect on the server but running it on the client can cause hydration mismatches if your effect modifies what gets rendered.

The useIsomorphicLayoutEffect Pattern

The community solution is a tiny helper that uses useEffect on the server and useLayoutEffect on the client:

import { useEffect, useLayoutEffect } from 'react';

const useIsomorphicLayoutEffect =
  typeof window !== 'undefined'
    ? useLayoutEffect
    : useEffect;

// Now use this instead
useIsomorphicLayoutEffect(() => {
  // Measure DOM, adjust layout, etc.
}, []);

This check detects whether you're running in a browser (window exists) or on the server. On the server, it falls back to useEffect, which React safely ignores during SSR without warnings.

When to Use This

Use useIsomorphicLayoutEffect when you need layout measurements that only make sense in the browser—like reading scroll position, measuring element dimensions, or preventing flicker—but your app also renders on the server.

Key Takeaway: useLayoutEffect fails on the server because there's no DOM; use the useIsomorphicLayoutEffect pattern to conditionally choose useEffect server-side and useLayoutEffect client-side, eliminating warnings while preserving synchronous behavior where it matters.