-
-
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.
Add support for React 18 server rendering (#1409)
* add support diff versions of React's render API in React 18, the API for rendering and hydrating elements is updated. This updates the syntax to support both syntaxes so that users of both APIs can still work with the same interface. * move files to reusables so that they can work for ReactOnRails * use ReactDOM.version to determine API for rendering and hydrating
- Loading branch information
1 parent
64f722f
commit 922e19c
Showing
5 changed files
with
41 additions
and
6 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 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,12 @@ | ||
import ReactDOM from 'react-dom'; | ||
import { ReactElement, Component } from 'react'; | ||
import supportsReactCreateRoot from './supportsReactCreateRoot'; | ||
|
||
export default function reactHydrate(domNode: Element, reactElement: ReactElement): void | Element | Component { | ||
if (supportsReactCreateRoot) { | ||
// @ts-expect-error potentially present if React 18 or greater | ||
return ReactDOM.hydrateRoot(domNode, reactElement); | ||
} | ||
|
||
return ReactDOM.hydrate(reactElement, domNode); | ||
} |
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,15 @@ | ||
import ReactDOM from 'react-dom'; | ||
import { ReactElement, Component } from 'react'; | ||
import supportsReactCreateRoot from './supportsReactCreateRoot'; | ||
|
||
export default function reactRender(domNode: Element, reactElement: ReactElement): void | Element | Component { | ||
if (supportsReactCreateRoot) { | ||
// @ts-expect-error potentially present if React 18 or greater | ||
const root = ReactDOM.createRoot(domNode); | ||
root.render(reactElement); | ||
return root | ||
} | ||
|
||
// eslint-disable-next-line react/no-render-return-value | ||
return ReactDOM.render(reactElement, domNode); | ||
} |
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,4 @@ | ||
import ReactDOM from 'react-dom'; | ||
|
||
const reactMajorVersion = parseInt(ReactDOM.version.split('.')[0], 10); | ||
export default reactMajorVersion >= 18; |