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

Warning in server side rendering #32

Open
Romcol opened this issue Apr 14, 2020 · 2 comments · May be fixed by #35
Open

Warning in server side rendering #32

Romcol opened this issue Apr 14, 2020 · 2 comments · May be fixed by #35

Comments

@Romcol
Copy link

Romcol commented Apr 14, 2020

Hi,

When using useComponentSize with server side rendering we experience this warning.

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.%s

https://i.ibb.co/WyRzDpg/uselayouteffect.png

The warning points to this ressource https://fb.me/react-uselayouteffect-ssr for correct handling on the server.

This is not major but should be better for those of us using SSR 😉

Anyways, thanks for lib!

@Magellol
Copy link

Magellol commented May 7, 2020

We're faced with the same issue. For now we've patched the local node-modules through patch-package

-let { useCallback, useState, useLayoutEffect } = require('react')
+let { useCallback, useState, useLayoutEffect, useEffect } = require('react')
+
+/**
+ * To properly measure. we need useLayoutEffect in the client but it generates a warning in the console
+ * since it has no effect when it runs on the server. This is to work around it.
+ * We've used this implementation: https://github.com/streamich/react-use/blob/master/src/useIsomorphicLayoutEffect.ts
+ * 
+ * See this issue for more details: https://github.com/rehooks/component-size/issues/32
+ */
+const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
 
 function getSize(el) {
   if (!el) {
@@ -29,7 +38,7 @@ function useComponentSize(ref) {
     [ref]
   )
 
-  useLayoutEffect(
+  useIsomorphicLayoutEffect(
     () => {
       if (!ref.current) {
         return

@swashata
Copy link

This is the patch for v1.0.3

diff --git a/node_modules/@rehooks/component-size/index.js b/node_modules/@rehooks/component-size/index.js
index 5e652a2..a161d61 100644
--- a/node_modules/@rehooks/component-size/index.js
+++ b/node_modules/@rehooks/component-size/index.js
@@ -3,6 +3,15 @@ var React = require('react')
 var useState = React.useState
 var useCallback = React.useCallback
 var useLayoutEffect = React.useLayoutEffect
+var useEffect = React.useEffect
+/**
+ * To properly measure. we need useLayoutEffect in the client but it generates a warning in the console
+ * since it has no effect when it runs on the server. This is to work around it.
+ * We've used this implementation: https://github.com/streamich/react-use/blob/master/src/useIsomorphicLayoutEffect.ts
+ *
+ * See this issue for more details: https://github.com/rehooks/component-size/issues/32
+ */
+var useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect
 
 function getSize(el) {
   if (!el) {
@@ -32,7 +41,7 @@ function useComponentSize(ref) {
     [ref]
   )
 
-  useLayoutEffect(
+  useIsomorphicLayoutEffect(
     function() {
       if (!ref.current) {
         return

@CruScanlan CruScanlan linked a pull request Feb 22, 2022 that will close this issue
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

Successfully merging a pull request may close this issue.

3 participants