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 React SRR useLayoutEffect console.error when using CompatRouter #9820

Merged
merged 2 commits into from
Jan 19, 2023

Conversation

brookslybrand
Copy link
Contributor

Currently when using react-router-dom-v5-compat's CompatRouter with SSR, you get this fun React warning in your console:

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://reactjs.org/link/uselayouteffect-ssr for common fixes.

This is something my team has ignored, but it's pretty obnoxious. Additionally, even Ryan believes this warning is unnecessary.

This PR simply adds a fix previous React core team member Bri implemented, as well as a test to verify that warning does not show up in a jest node environment

@changeset-bot
Copy link

changeset-bot bot commented Jan 5, 2023

⚠️ No Changeset found

Latest commit: 84a3484

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Jan 5, 2023

Hi @brookslybrand,

Welcome, and thank you for contributing to React Router!

Before we consider your pull request, we ask that you sign our Contributor License Agreement (CLA). We require this only once.

You may review the CLA and sign it by adding your name to contributors.yml.

Once the CLA is signed, the CLA Signed label will be added to the pull request.

If you have already signed the CLA and received this response in error, or if you have any questions, please contact us at hello@remix.run.

Thanks!

- The Remix team

Comment on lines +1 to +3
/**
* @jest-environment node
*/
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If there is a better/preferred way to set the environment in jest, please let me know 🙂. Without this comment this test will have a false-positive, since it assumes we are in a browser environment where this isn't a problem.

Comment on lines +9 to +13
// Have to mock react-router-dom to have a comparable API to v5, otherwise it will
// be using v6's API and fail
jest.mock("react-router-dom", () => ({
useHistory: () => ({ location: "/" }),
}));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't super love having to mock this out, but without this mock CompatRouter was trying to use useHistory from the react-router-dom version it had (v6, where it doesn't exist). Since this test is pretty simple I figured this would be fine, but let me know otherwise.

Comment on lines +26 to +36
// Copied with 💜 from https://github.com/bvaughn/react-resizable-panels/blob/main/packages/react-resizable-panels/src/hooks/useIsomorphicEffect.ts
const canUseEffectHooks = !!(
typeof window !== "undefined" &&
typeof window.document !== "undefined" &&
typeof window.document.createElement !== "undefined"
);

const useIsomorphicLayoutEffect = canUseEffectHooks
? React.useLayoutEffect
: () => {};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the words of @ryanflorence

The warning react throws is so dang stupid. We shouldn’t need this.

@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Jan 5, 2023

Thank you for signing the Contributor License Agreement. Let's get this merged! 🥳

@brophdawg11
Copy link
Contributor

Thanks! Confirmed fthis is good with Ryan as well 👍

@brophdawg11 brophdawg11 merged commit 2e26339 into remix-run:dev Jan 19, 2023
@brookslybrand brookslybrand deleted the compat-ssr-warning branch January 19, 2023 21:38
@brookslybrand
Copy link
Contributor Author

Thanks! Confirmed fthis is good with Ryan as well 👍

Sweet! Thanks 😄

@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version 6.7.1-pre.0 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version 6.8.0 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants