-
-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require createRoot/hydrateRoot from 'react-dom/client' under react-do…
…m 18+ Fixes #1441
- Loading branch information
Showing
10 changed files
with
56 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { ReactElement, Component } from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
type HydrateOrRenderReturnType = void | Element | Component; | ||
type HydrateOrRenderType = (domNode: Element, reactElement: ReactElement) => HydrateOrRenderReturnType; | ||
const supportsReactCreateRoot = parseInt(ReactDOM.version.split('.')[0], 10) >= 18; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let reactDomClient: any; | ||
if (supportsReactCreateRoot) { | ||
if (require.context) { | ||
// we are in a Webpack environment (and in a node-modules directory of another project) | ||
// See https://webpack.js.org/guides/dependency-management/#context-module-api | ||
const reactDomContext = require.context('../react-dom/', false, /^client\.js$/); | ||
reactDomClient = reactDomContext('../react-dom/client.js'); | ||
} else { | ||
// we want a dynamic require here so that webpack doesn't rewrite it | ||
const reactDomClientName = 'react-dom/client'; | ||
// eslint-disable-next-line global-require,import/no-dynamic-require | ||
reactDomClient = require(reactDomClientName); | ||
} | ||
} | ||
|
||
export const reactHydrate: HydrateOrRenderType = supportsReactCreateRoot ? | ||
reactDomClient.hydrateRoot : | ||
(domNode, reactElement) => ReactDOM.hydrate(reactElement, domNode); | ||
|
||
export function reactRender(domNode: Element, reactElement: ReactElement): HydrateOrRenderReturnType { | ||
if (supportsReactCreateRoot) { | ||
const root = reactDomClient.createRoot(domNode); | ||
root.render(reactElement); | ||
return root; | ||
} | ||
|
||
// eslint-disable-next-line react/no-render-return-value | ||
return ReactDOM.render(reactElement, domNode); | ||
} | ||
|
||
export default function reactHydrateOrRender(shouldHydrate: boolean, domNode: Element, reactElement: ReactElement): HydrateOrRenderReturnType { | ||
return shouldHydrate ? reactHydrate(domNode, reactElement) : reactRender(domNode, reactElement); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6349,7 +6349,7 @@ react-is@^16.7.0: | |
integrity sha512-rPCkf/mWBtKc97aLL9/txD8DZdemK0vkA3JMLShjlJB3Pj3s+lpf1KaBzMfQrAmhMQB0n1cU/SUGgKKBCe837Q== | ||
|
||
"react-on-rails@file:.yalc/react-on-rails": | ||
version "13.0.1" | ||
version "13.0.2" | ||
dependencies: | ||
"@babel/runtime-corejs3" "^7.12.5" | ||
concurrently "^5.1.0" | ||
|
@@ -6983,11 +6983,10 @@ sha.js@^2.4.0, sha.js@^2.4.8: | |
inherits "^2.0.1" | ||
safe-buffer "^5.0.1" | ||
|
||
[email protected]: | ||
name shakapacker | ||
version "6.4.0" | ||
resolved "https://registry.yarnpkg.com/shakapacker/-/shakapacker-6.4.0.tgz#f49ad8bb51eb1a151b190b588ad467c6e6f62e69" | ||
integrity sha512-bxxi7Lla3n2fnFjxVv6BtD2vcTEZIm2eb29WkJqZBmpFVT9tnwbd3XQISD9eOst6kTEJu2ruUp7dGvuEjpxwkg== | ||
shakapacker@^6.4.0: | ||
version "6.4.1" | ||
resolved "https://registry.yarnpkg.com/shakapacker/-/shakapacker-6.4.1.tgz#4c3662c63acf54ecd7847f97ceb88ee1a23bfa2a" | ||
integrity sha512-yCGqDVNP5Dz4W8wAl53M/zG6/xpvAaFItPVVlDuHyq3xCzHD2MYEBvmITNgBvvl2WU34OPcEIeVrXOWcl/MCMQ== | ||
dependencies: | ||
glob "^7.2.0" | ||
js-yaml "^4.1.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters