Skip to main content
InterviewHubPRO
Section Overview
Q291

Question 291 of 407

Part 9: React Hooks Deep Dive (262 - 316)

71%
React Core & Hooks (407 Qs)•Part 9: React Hooks Deep Dive (262 - 316)

How does useLayoutEffect work during server-side rendering (SSR)?

mid LevelCode ExampleHooksSSR & Hydration

The useLayoutEffect hook does not run on the server, because there is no DOM. React issues a warning in server environments like Next.js if useLayoutEffect is used directly.

This can be mitigated using a conditional polyfill:

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

i.e, Use useIsomorphicLayoutEffect in components that render both on client and server.