-
Notifications
You must be signed in to change notification settings - Fork 752
Load react-dom correctly based on React version #1269
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
Conversation
With React 18, the rendering interface will be updated in order to benefit from new features. reactwg/react-18#5 This allows React 18 rendering to be supported while creating backwards compatibility. For the time being, the return of the components does not change the output from the previous versions so as to not cause any breaking changes.
The code comment pointing to webpack/webpack#339 (comment) specifies that:
This statement is correct, i.e. our frontend code compiles without error, but the warning is displayed as a transparent overlay when running webpacker dev server. It is also constantly printed to our logs. Is there a more elegant way than shutting webpacker up via config? Why is it trying to even import |
@gregorbg Do you mean you still get the warning message in v2.7? |
I am specifically saying that we get the error only on v2.7 We are using React and React-DOM v16.14.0 and our code is at https://github.com/thewca/worldcubeassociation.org. To reproduce, literally just spin up the Docker container with |
Hi @gregorbg, yes, the problem is crazy annoying. Here's the solution: https://github.com/shakacode/react_on_rails/blob/master/docs/guides/rails-webpacker-react-integration-options.md?plain=1#L59-L86 @ahangarha can please double check:
CC: @Judahmeek |
Thanks for pointing me in the right direction! However, I can't help but wonder the following: The webpacker error message points me to import ReactDOM from "react-dom"
const reactMajorVersion = ReactDOM.version?.split('.')[0] || 16
// TODO: once we require React 18, we can remove this and inline everything guarded by it.
const supportsRootApi = reactMajorVersion >= 18
let reactDomClient = ReactDOM
if (supportsRootApi) {
// This will never throw an exception, but it's the way to tell Webpack the dependency is optional
// https://github.com/webpack/webpack/issues/339#issuecomment-47739112
// Unfortunately, it only converts the error to a warning.
try {
// eslint-disable-next-line global-require,import/no-unresolved
reactDomClient = require('react-dom/client');
} catch (e) {
// We should never get here, but if we do, we'll just use the default ReactDOM
// and live with the warning.
reactDomClient = ReactDOM;
}
}
export default reactDomClient where the relevant line is I can certainly implement the fix that you mentioned (and pointed us to in thewca/worldcubeassociation.org#7904, thanks a lot! 😄) but why is this warning even being shown in the first place? |
Thanks @gregorbg for your feedback. |
This change appears to break SSR. See #1276. |
Summary
This PR fixes the warning related to loading
react-dom
in React 18.Pull Request checklist
Add/update test to cover these changesUpdate documentationOther Information
Closes #1258