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

Require createRoot/hydrateRoot from 'react-dom/client' under react-dom 18+ #1460

Merged
merged 1 commit into from
Jun 29, 2022

Conversation

alexeyr
Copy link
Contributor

@alexeyr alexeyr commented Jun 22, 2022

Fixes #1441


This change is Reviewable

@alexeyr
Copy link
Contributor Author

alexeyr commented Jun 23, 2022

@tomdracz @summera Can you take a look at this one? Especially if you have actual React 18 apps to test it with? OTOH, some generated examples use React 18 and the warning doesn't seem to be in the logs https://github.com/shakacode/react_on_rails/runs/7024950174?check_suite_focus=true

@alexeyr-ci1 alexeyr-ci1 force-pushed the alexeyr/conditional-react18-require branch 2 times, most recently from c705aa6 to fcfbee9 Compare June 23, 2022 15:46
@justin808
Copy link
Member

@alexeyr LGTM!

Copy link
Contributor

@tomdracz tomdracz left a comment

Choose a reason for hiding this comment

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

LGTM, not entirely sure still why this one works where the other approaches didn't but 🤷‍♂️

Don't have React 18 app on hand to test though, if we have test cases that use it, it's good enough

let reactDomClient: any;
if (supportsReactCreateRoot) {
// eslint-disable-next-line camelcase
if (__webpack_require__) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Wonder if this is the magic thing that was missing in the other attempts!

Copy link
Contributor

@alexeyr-ci1 alexeyr-ci1 Jun 24, 2022

Choose a reason for hiding this comment

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

No, it isn't :) If I understand correctly (Webpack documentation is bad), #1448 was actually close to working, but Webpack needs a base directory for dynamic imports/requires. If it had

require(`react-dom/${supportsReactCreateRoot ? 'client' : 'index'}`);

it would recognize react-dom as the base directory and work.

Copy link
Contributor

Choose a reason for hiding this comment

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

This if is needed because yarn run test in react_on_rails doesn't run under Webpack and require.context is not available.

Copy link
Contributor

@alexeyr-ci1 alexeyr-ci1 Jun 24, 2022

Choose a reason for hiding this comment

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

Don't have React 18 app on hand to test though, if we have test cases that use it, it's good enough

I've tried to update spec/dummy to React 18 and it doesn't work after all. Maybe the generated examples don't use Webpack?

Copy link
Member

Choose a reason for hiding this comment

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

How about if we just use a different import for react 18 with react on rails?

Check source for react-rails?

Google to see what other libraries do which have react as a dependency?

Copy link
Contributor

Choose a reason for hiding this comment

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

Some of the libraries I've seen that got direct dependency react-dom seem to have just released major versions that only support React 18 and left it at that. For all intents and purposes, React 18 components not initialized with the new root API should function the same as they do in React 17.

I do feel there's bit more at play here, think I've noted some other changes that might be required at one of the other PRs trying to tackle this.

@alexeyr-ci1 alexeyr-ci1 force-pushed the alexeyr/conditional-react18-require branch from c6d157f to 23d52e0 Compare June 27, 2022 08:46
@alexeyr-ci1 alexeyr-ci1 force-pushed the alexeyr/conditional-react18-require branch from 23d52e0 to e7339ff Compare June 27, 2022 09:08
@alexeyr-ci1 alexeyr-ci1 force-pushed the alexeyr/conditional-react18-require branch from e7339ff to 61f7df8 Compare June 27, 2022 09:12
@alexeyr-ci1
Copy link
Contributor

@tomdracz @justin808 The actual solution turns out to be entirely different. Please review again. See #1463 for the React 18 test.

@alexeyr-ci1 alexeyr-ci1 marked this pull request as ready for review June 27, 2022 11:31
@alexeyr-ci1
Copy link
Contributor

build-and-test CI is a lot slower (but not as much as in #1463), not clear if it's permanent or corresponds to an actual slow-down.

@alexeyr-ci1 alexeyr-ci1 merged commit 35d9d69 into master Jun 29, 2022
@alexeyr-ci1 alexeyr-ci1 deleted the alexeyr/conditional-react18-require branch June 29, 2022 19:33
@ ./node_modules/react-on-rails/node_package/lib/ReactOnRails.js 34:45-78
@ ./client/app/packs/client-bundle.js 5:0-42 32:0-23 35:0-21 59:0-26
```
It can be safely [suppressed](https://webpack.js.org/configuration/other-options/#ignorewarnings) in your Webpack configuration.
Copy link
Member

Choose a reason for hiding this comment

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

@ahangarha Let's better document this.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am concerning about version 16 or 18. I think there is a mistake there. I asked Alexey for clarification.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, I mentioned a fix for the phrasing.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We could also put it into the config shakapacker exports. Would that potentially break anything? @justin808

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