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

fix(HeightAnimation): avoid usage of useLayoutEffect during SSR #1749

Merged
merged 1 commit into from
Nov 24, 2022

Conversation

tujoworker
Copy link
Member

React warns about the usage of useLayoutEffect when React renders on the server (SSR).

This behavior can be enabled with the FAST_DEV flag in gatsby-config.

One "quick" fix is to use useEffect on the server, which again does nothing there.

The React "team" has some options to that "problem". We basically use option 1, with this approach.

Any other suggestions?

PS: I tried to create a test. But invalidating both the document or the window object (delete globalThis.window) makes the testing lib not rendering the component or hook.

React warns about the usage of `useLayoutEffect` when React renders on the server (SSR).

This behavior can be enabled with the `FAST_DEV` flag in gatsby-config.

One "quick" fix is to use useEffect on the server, which again does nothing there.

The React team has [some options](https://gist.github.com/gaearon/e7d97cdf38a2907924ea12e4ebdf3c85) to that "problem". We basically use option 1, with this approach.

Any other suggestions?
@tujoworker tujoworker force-pushed the fix/ssr-usage-of-layout-effect branch from 8911e5c to ce60b3a Compare November 23, 2022 12:50
@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit ce60b3a:

Sandbox Source
eufemia-starter Configuration

@gatsby-cloud
Copy link

gatsby-cloud bot commented Nov 23, 2022

✅ DNB Eufemia Portal deploy preview ready

@tujoworker tujoworker requested a review from langz November 23, 2022 13:27
Copy link
Contributor

@langz langz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation.
I also think option 1 seems to be the the best, as long as the effect isn't important for first render.

I didn't have time to test this now, but can we experience/see a difference in the components that use our hooks from useHeightAnimation.tsx?

@tujoworker tujoworker merged commit 667059b into main Nov 24, 2022
@tujoworker tujoworker deleted the fix/ssr-usage-of-layout-effect branch November 24, 2022 11:07
@tujoworker
Copy link
Member Author

I did check it and could not find any 👍 But I also think it is just fine, because on SSR, there is no difference and on the browser, with re-hydration, useLayoutEffect will still run before any useEffect, which is important for that specific use case.

tujoworker pushed a commit that referenced this pull request Nov 24, 2022
## [9.39.1](v9.39.0...v9.39.1) (2022-11-24)

### Bug Fixes

* **HeightAnimation:** avoid usage of useLayoutEffect during SSR ([#1749](#1749)) ([667059b](667059b))
@tujoworker
Copy link
Member Author

🎉 This PR is included in version 9.39.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

joakbjerk pushed a commit that referenced this pull request Mar 27, 2023
React warns about the usage of `useLayoutEffect` when React renders on the server (SSR).

This behavior can be enabled with the `FAST_DEV` flag in gatsby-config.

One "quick" fix is to use useEffect on the server, which again does nothing there.

The React team has [some options](https://gist.github.com/gaearon/e7d97cdf38a2907924ea12e4ebdf3c85) to that "problem". We basically use option 1, with this approach.

Any other suggestions?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

2 participants