Skip to content

Commit f051579

Browse files
committed
Load react-dom correctly based on react version
1 parent 335eeb0 commit f051579

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

react_ujs/src/reactDomClient.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import ReactDOM from "react-dom"
2+
3+
const reactMajorVersion = ReactDOM.version?.split('.')[0] || 16
4+
5+
// TODO: once we require React 18, we can remove this and inline everything guarded by it.
6+
const supportsRootApi = reactMajorVersion >= 18
7+
8+
let reactDomClient = ReactDOM
9+
10+
if (supportsRootApi) {
11+
// This will never throw an exception, but it's the way to tell Webpack the dependency is optional
12+
// https://github.com/webpack/webpack/issues/339#issuecomment-47739112
13+
// Unfortunately, it only converts the error to a warning.
14+
try {
15+
// eslint-disable-next-line global-require,import/no-unresolved
16+
reactDomClient = require('react-dom/client');
17+
} catch (e) {
18+
// We should never get here, but if we do, we'll just use the default ReactDOM
19+
// and live with the warning.
20+
reactDomClient = ReactDOM;
21+
}
22+
}
23+
24+
export default reactDomClient

react_ujs/src/renderHelpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ReactDOM from "react-dom"
1+
import ReactDOM from "./reactDomClient"
22

33
export function supportsHydration() {
44
return typeof ReactDOM.hydrate === "function" || typeof ReactDOM.hydrateRoot === "function"

0 commit comments

Comments
 (0)