question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Overlay causes `useLayoutEffect` warnings in SSR mode

See original GitHub issue

Describe the bug

When using the <Overlay /> and components that build upon it in SSR mode, React emits warnings about calling useLayoutEffect during SSR.

Of course, there are legitimate uses of useLayoutEffect in Primer and cases where it may be unavoidable. However, in <Overlay />, it looks like this particular call is not doing any DOM measurement or direct manipulation, but is kicking off an animation. This seems like it’s probably fine to defer until after a paint occurs, and I’m guessing that browser is probably going to do that anyway before this animation begins.

Given that, I wonder if we can move this to a useEffect call, instead or alternatively investigate using a useIsomorphicLayoutEffect, which @mattcosta7 reminded me about in Slack:

const useIsomorphicLayoutEffect = typeof document !== 'undefined' ? useLayoutEffect : useEffect

(That said, in this case, I think just using the simpler useEffect is fine.)

In order to avoid these warnings currently, I have to defer completely the rendering of overlays until we’re on the client.

To Reproduce Steps to reproduce the behavior:

  1. Create a Next.js app
  2. Render an <Overlay /> on some page
  3. Watch logs

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
pksjcecommented, Nov 3, 2021

Was able to reproduce this here https://github.com/pksjce/nextjs-primer-react

Looking at ways to mitigate this!

1reaction
jclemcommented, Oct 12, 2021

I’ll have to go back a bit and find a commit this was still happening. Sorry for the delay, but I should be able to get to it in the next couple of days.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useLayoutEffect is causing warnings in SSR #295 - GitHub
Warning : useLayoutEffect does nothing on the server, because its effect cannot be encoded into the server renderer's output format.
Read more >
useLayoutEffect and SSR - Medium
useEffect and useLayoutEffect are React hooks that allow for the creation of side effects. ... '`useLayoutEffect` causes a warning in SSR.
Read more >
usePreventScroll causes useLayoutEffect warning in Nextjs
js and I'm trying to integrate the @react-aria/overlays package in my project. I have a layout component, where I'm simply invoking the ...
Read more >
useLayoutEffect and the warning on SSR - Eight Bites
Warning : useLayoutEffect does nothing on the server, because its effect cannot be encoded into the server renderer's output format.
Read more >
Change Log - Ant Design
Fix Typography useLayoutEffect warning in SSR. #33818 @SoYoung210. Fix Typography with ellipsis makes screen show the scroll bar in some case.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found