Theme-ui: `useLayoutEffect` and SSR mismatch

Created on 18 Jun 2019  路  3Comments  路  Source: system-ui/theme-ui

Hi! This is looking great.

I ran into an issue while setting up theme-ui in a Next.js project which is rendered on the server.

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://fb.me/react-uselayouteffect-ssr for common fixes.

https://github.com/system-ui/theme-ui/blob/b782a1d51f676e009e09e8c361b4c19b96cc685c/packages/theme-ui/src/color-modes.js#L28-L39 appears to be the culprit.

According to the link referenced in the error, we'd either need to push this to useEffect (sounds undesirable), or, similar to what redux does, create a useIsomorphicLayoutEffect like this:

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

thoughts?

Most helpful comment

just wanted to mention that this is still on my mind and I've been kicking it around

All 3 comments

Yeah, I did try to switch back to useEffect which I think is the right move tbh. There was an issue with the order of localStoarage calls that needs to be updated as well. If you wanna take a stab or look into this, that鈥檇 be super helpful! Also, if you鈥檇 like to add something similar to the gatsby plugin to prevent the visual flash in Next.js, that鈥檚 definitely something that鈥檇 be a great addition to the library

just wanted to mention that this is still on my mind and I've been kicking it around

@VinSpee going to close this issue in favor of #270

Was this page helpful?
0 / 5 - 0 ratings

Related issues

george-norris-salesforce picture george-norris-salesforce  路  3Comments

tesseralis picture tesseralis  路  3Comments

Everspace picture Everspace  路  3Comments

calvinwyoung picture calvinwyoung  路  3Comments

8eecf0d2 picture 8eecf0d2  路  3Comments