-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: properly handling global react-dom@18
- Loading branch information
1 parent
4146244
commit 2376cae
Showing
4 changed files
with
53 additions
and
53 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,34 @@ | ||
import * as ReactDOM from 'react-dom'; | ||
import type { createRoot as createRootOriginal } from 'react-dom/client'; | ||
|
||
type ReactDOM18 = ReactDOMOriginal & { | ||
createRoot?: CreateRoot; | ||
}; | ||
|
||
let MaybeReactDOM18: ReactDOM18; | ||
try { | ||
// eslint-disable-next-line global-require,@typescript-eslint/no-var-requires | ||
MaybeReactDOM18 = require('react-dom/client') as ReactDOM18; | ||
} catch { | ||
MaybeReactDOM18 = ReactDOM as ReactDOM18; | ||
} | ||
|
||
type CreateRoot = typeof createRootOriginal; | ||
type CreateRootParams = Parameters<CreateRoot>; | ||
type ReactDOMOriginal = typeof ReactDOM; | ||
type RendererProps = Parameters<typeof ReactDOM['render']>; | ||
|
||
const createRootFake = (container: CreateRootParams[0]) => { | ||
const newRoot = { | ||
render: (element: RendererProps[0]) => { | ||
ReactDOM.render(element, container); | ||
}, | ||
unmount: () => { | ||
ReactDOM.unmountComponentAtNode(container); | ||
}, | ||
}; | ||
return newRoot; | ||
}; | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export const { createRoot = createRootFake } = MaybeReactDOM18; |