1- /* eslint-disable global-require,@typescript-eslint/no-require-imports */
2- import * as ReactDOM from 'react-dom' ;
1+ /* eslint-disable global-require,@typescript-eslint/no-require-imports,@typescript-eslint/no-deprecated,@typescript-eslint/no-non-null-assertion,react/no-deprecated --
2+ * while we need to support React 16
3+ */
4+ import { version , hydrate , render , unmountComponentAtNode as unmountComponentAtNodeLib } from 'react-dom' ;
35import type { ReactElement } from 'react' ;
46import type { RenderReturnType } from './types/index.ts' with { 'resolution-mode' : 'import' } ;
57
6- const reactMajorVersion = Number ( ReactDOM . version ?. split ( '.' ) [ 0 ] ) || 16 ;
8+ const reactMajorVersion = Number ( version ?. split ( '.' ) [ 0 ] ) || 16 ;
79
810// TODO: once we require React 18, we can remove this and inline everything guarded by it.
911export const supportsRootApi = reactMajorVersion >= 18 ;
1012
11- export const supportsHydrate = supportsRootApi || ' hydrate' in ReactDOM ;
13+ export const supportsHydrate = supportsRootApi || ! ! hydrate ;
1214
1315// TODO: once React dependency is updated to >= 18, we can remove this and just
1416// import ReactDOM from 'react-dom/client';
@@ -20,20 +22,15 @@ if (supportsRootApi) {
2022 try {
2123 reactDomClient = require ( 'react-dom/client' ) as typeof import ( 'react-dom/client' ) ;
2224 } catch ( _e ) {
23- // We should never get here, but if we do, we'll just use the default ReactDOM
24- // and live with the warning.
25- reactDomClient = ReactDOM as unknown as typeof import ( 'react-dom/client' ) ;
25+ // do nothing
2626 }
2727}
2828
2929type HydrateOrRenderType = ( domNode : Element , reactElement : ReactElement ) => RenderReturnType ;
3030
31- /* eslint-disable @typescript-eslint/no-deprecated,@typescript-eslint/no-non-null-assertion,react/no-deprecated --
32- * while we need to support React 16
33- */
3431export const reactHydrate : HydrateOrRenderType = supportsRootApi
3532 ? reactDomClient ! . hydrateRoot
36- : ( domNode , reactElement ) => ReactDOM . hydrate ( reactElement , domNode ) ;
33+ : ( domNode , reactElement ) => hydrate ( reactElement , domNode ) ;
3734
3835export function reactRender ( domNode : Element , reactElement : ReactElement ) : RenderReturnType {
3936 if ( supportsRootApi ) {
@@ -42,11 +39,10 @@ export function reactRender(domNode: Element, reactElement: ReactElement): Rende
4239 return root ;
4340 }
4441
45- // eslint-disable-next-line react/no-render-return-value
46- return ReactDOM . render ( reactElement , domNode ) ;
42+ return render ( reactElement , domNode ) ;
4743}
4844
49- export const unmountComponentAtNode : typeof ReactDOM . unmountComponentAtNode = supportsRootApi
45+ export const unmountComponentAtNode : typeof unmountComponentAtNodeLib = supportsRootApi
5046 ? // not used if we use root API
5147 ( ) => false
52- : ReactDOM . unmountComponentAtNode ;
48+ : unmountComponentAtNodeLib ;
0 commit comments