Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useLayoutEffect is causing warnings in SSR #295

Closed
snelsi opened this issue Aug 11, 2021 · 1 comment
Closed

useLayoutEffect is causing warnings in SSR #295

snelsi opened this issue Aug 11, 2021 · 1 comment

Comments

@snelsi
Copy link

snelsi commented Aug 11, 2021

Following #288

Summary

useLayoutEffect is causing console warnings when using in SSR

Background

I'm using useCookies hook in a Next.js application. After updating to 4.1.0 I'm getting this error:

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://reactjs.org/link/uselayouteffect-ssr for common fixes.

Possible solution

Swapping useLayoutEffect with this custom useIsomorphicLayoutEffect might help:

const isHopefullyDomEnvironment =
  typeof window !== 'undefined' &&
  typeof window.document !== 'undefined' &&
  typeof window.document.createElement !== 'undefined'

export const useIsomorphicLayoutEffect = isHopefullyDomEnvironment
  ? useLayoutEffect
  : useEffect

Check out this PR for more info: reduxjs/react-redux#1444

@eXon
Copy link
Collaborator

eXon commented Aug 12, 2021

The server-side is now skipping it since the change event does not need to be used on SSR. Released in v4.1.1

@eXon eXon closed this as completed Aug 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants