useIsomorphicLayoutEffect
Uses `useLayoutEffect` in SSR and `useEffect` in CSR.
Check the console
Installation
Copy-paste the hook
Copy and paste the hook code in a .ts
file.
Usage
Details
Isomorphic behavior
useIsomorphicLayoutEffect
ensures thatuseLayoutEffect
is only used in client-side rendering (CSR) environments where the DOM is available. In server-side rendering (SSR), it safely falls back touseEffect
to prevent React warnings.- This behavior helps avoid issues with server-side rendering since
useLayoutEffect
runs synchronously after all DOM mutations and can’t be used on the server where there’s no DOM.
Synchronous vs Asynchronous effects
- On the client, the hook behaves like
useLayoutEffect
, meaning it runs synchronously after all DOM changes but before the browser has a chance to paint. This is useful for tasks like measuring layout, handling scroll positions, or making DOM manipulations. - On the server, it behaves like
useEffect
, which is asynchronous and won’t block rendering. This allows the server to execute logic safely without worrying about DOM access.