-
-
Notifications
You must be signed in to change notification settings - Fork 631
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,44 @@ | ||
import type { ReactElement } from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import type { RenderReturnType } from './types'; | ||
|
||
type HydrateOrRenderType = (domNode: Element, reactElement: ReactElement) => RenderReturnType; | ||
const supportsReactCreateRoot = ReactDOM.version && | ||
parseInt(ReactDOM.version.split('.')[0], 10) >= 18; | ||
|
||
// TODO: once React dependency is updated to >= 18, we can remove this and just | ||
// import ReactDOM from 'react-dom/client'; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let reactDomClient: any; | ||
if (supportsReactCreateRoot) { | ||
// 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 const reactHydrate: HydrateOrRenderType = supportsReactCreateRoot ? | ||
reactDomClient.hydrateRoot : | ||
(domNode, reactElement) => ReactDOM.hydrate(reactElement, domNode); | ||
|
||
export function reactRender(domNode: Element, reactElement: ReactElement): RenderReturnType { | ||
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(domNode: Element, reactElement: ReactElement, hydrate: boolean): RenderReturnType { | ||
return hydrate ? reactHydrate(domNode, reactElement) : reactRender(domNode, reactElement); | ||
} |
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 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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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