I'm using react-static to generate a static website. Using useLayoutEffect
from the new hook API, I get this warning during the static rendering phase (same API as server-side rendering) :
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 th e intended UI.
To avoid this, useLayoutEffect should only be used in components that render exclusively on the client.
Of course this makes sense. But what's the good option to get rid of this warning when one's sure there won't be any mismatch ?
In my layout effect I'm only adding a bit of CSS to the body
tag, so there won't be any mismatch during the hydration phase on the client (since body
isn't React's business).
React strongly forbids using conditional hooks, but in that very specific case, wouldn't it make sense to do something like :
if(typeof window !== 'undefined')
useLayoutEffect(() => {
document.body.style.overflowY = loading ? 'hidden' : 'visible'
},
[loading]
)
What's the proper way ?