use-tweaks icon indicating copy to clipboard operation
use-tweaks copied to clipboard

fix: useEffect when SSR

Open Kerumen opened this issue 5 years ago • 5 comments

When SSR, useLayoutEffect prints a warning. This PR fixes this by swapping the hook with a useEffect if we are on the server. This is a common way to solve this problem:

  • https://github.com/streamich/react-use/blob/6737092ad9260ca1fe481bf4c10ffa650ae35476/src/useIsomorphicLayoutEffect.ts
  • https://github.com/mui-org/material-ui/blob/e53a9543cf28b67be6216e5f0ffd99be553cb258/packages/material-ui-utils/src/useEnhancedEffect.js

Screenshot 2020-11-06 at 11 00 15

Kerumen avatar Nov 06 '20 10:11 Kerumen

+1 on this, makes a lot of noise in the log when developing with Next.js

mysterybear avatar Feb 19 '21 09:02 mysterybear

@mysterybear can you test the PR and give feedback? I'll merge then 👍

gsimone avatar Feb 19 '21 11:02 gsimone

Hi @gsimone thanks for quick reply, I've tested using JS and it seems to work fine here: https://codesandbox.io/s/github/mysterybear/tweaks-next-ssr-use-effect-test?file=/pages/index.js

I just took the example codesandbox code and pasted it into a Next.js project and pointed use-tweaks at the patch author's branch, hope that's okay

Edit: wanted to try with TS too, but getting the same problem as #43 even with v0.3.1

mysterybear avatar Feb 19 '21 15:02 mysterybear

Any chance this gets ever merged ?

enzoferey avatar Jun 07 '21 09:06 enzoferey

Hey @enzoferey (and @mysterybear), we pretty much moved all efforts to https://github.com/pmndrs/leva; if you want to help with use-tweaks, let me know and I can make you maintainer 👍

gsimone avatar Jun 07 '21 10:06 gsimone