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.

[BUG] useLayoutEffect warning when rendering serverside

See original GitHub issue

The change in #195 introduces useLayoutEffect to the the useViewportScroll (and the new useElementScroll) hook, that causes a React warning when rendering serverside.

See CodeSandbox

Warning: useLayoutEffect does nothing on the server, because its effect cannot be encoded into the server renderer's output format. This will lead to a mismatch between the initial, non-hydrated UI and the intended UI. To avoid this, useLayoutEffect should only be used in components that render exclusively on the client. See https://fb.me/react-uselayouteffect-ssr for common fixes.

A common fix is to create a useIsomorphicLayoutEffect that selects the right effect for the env.

const useIsomorphicLayoutEffect = isBrowser ? useLayoutEffect : useEffect

function Comp() {
  useIsomorphicLayoutEffect(() => {
    // ...
  })
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:14
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

14reactions
seawattscommented, Feb 10, 2022

I’m using NextJs 12 and Framer 6.2.6 and am seeing this error.

12reactions
Olovyannikovcommented, Feb 13, 2022

I solved this problem with:

const [isLoaded, setLoaded] = useState(false);
  
  useEffect(() => {
    setLoaded(true);
  }, []);

  if (!isLoaded) {
    return <></>;
  }
  
  return (
    <AnimatePresence exitBeforeEnter>
      // ....code
    </AnimatePresence>
  )
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix the "Warning: useLayoutEffect does nothing on the ...
Heres the full error: Warning: useLayoutEffect does nothing on the server, because its effect cannot be encoded into the server renderer's ...
Read more >
useLayoutEffect and SSR - Medium
useLayoutEffect logs a nasty warning in server-side rendering. You can hack around this warning by conditionally using useEffect in server ...
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 >
Module Constraints - Remix
Browser-only code - Remix renders on the server so your modules can't have ... set in a useLayoutEffect in your elements and you...
Read more >
React useLayoutEffect vs. useEffect with examples
Compare the useEffect and useLayoutEffect Hooks in React, ... passed to useEffect will run after the render is committed to the screen”.
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