useIsomorphicLayoutEffect is a safe drop‑in replacement for useLayoutEffect that automatically falls back to useEffect during SSR (server‑side rendering).
- In the browser, it behaves exactly like
useLayoutEffect. - On the server, it uses
useEffect, preventing the React warning: "useLayoutEffect does nothing on the server".
export const useIsomorphicLayoutEffect: typeof useLayoutEffect | typeof useEffect;import { useIsomorphicLayoutEffect } from '@webeach/react-hooks/useIsomorphicLayoutEffect';
function Component() {
useIsomorphicLayoutEffect(() => {
console.log('Runs layout effect only in the browser');
}, []);
return <div />;
}- When building components that must work consistently both in the browser and on the server.
- To avoid React warnings during server‑side rendering while still performing synchronous DOM‑related work in the browser.
- In projects without SSR (you can use
useLayoutEffectdirectly). - When plain
useEffectis sufficient and synchronous DOM work is not required.
-
Ignoring SSR warnings
- Using
useLayoutEffectdirectly in code that runs on the server can trigger warnings in React. UseuseIsomorphicLayoutEffectto silence them safely.
- Using
-
Mixing it up with
useEffect- Choose
useIsomorphicLayoutEffectwhen you need layout‑phase effects in the browser and safe behavior during SSR. Otherwise preferuseEffect.
- Choose