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' ;
3
5
import type { ReactElement } from 'react' ;
4
6
import type { RenderReturnType } from './types/index.ts' with { 'resolution-mode' : 'import' } ;
5
7
6
- const reactMajorVersion = Number ( ReactDOM . version ?. split ( '.' ) [ 0 ] ) || 16 ;
8
+ const reactMajorVersion = Number ( version ?. split ( '.' ) [ 0 ] ) || 16 ;
7
9
8
10
// TODO: once we require React 18, we can remove this and inline everything guarded by it.
9
11
export const supportsRootApi = reactMajorVersion >= 18 ;
10
12
11
- export const supportsHydrate = supportsRootApi || ' hydrate' in ReactDOM ;
13
+ export const supportsHydrate = supportsRootApi || ! ! hydrate ;
12
14
13
15
// TODO: once React dependency is updated to >= 18, we can remove this and just
14
16
// import ReactDOM from 'react-dom/client';
@@ -20,20 +22,15 @@ if (supportsRootApi) {
20
22
try {
21
23
reactDomClient = require ( 'react-dom/client' ) as typeof import ( 'react-dom/client' ) ;
22
24
} 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
26
26
}
27
27
}
28
28
29
29
type HydrateOrRenderType = ( domNode : Element , reactElement : ReactElement ) => RenderReturnType ;
30
30
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
- */
34
31
export const reactHydrate : HydrateOrRenderType = supportsRootApi
35
32
? reactDomClient ! . hydrateRoot
36
- : ( domNode , reactElement ) => ReactDOM . hydrate ( reactElement , domNode ) ;
33
+ : ( domNode , reactElement ) => hydrate ( reactElement , domNode ) ;
37
34
38
35
export function reactRender ( domNode : Element , reactElement : ReactElement ) : RenderReturnType {
39
36
if ( supportsRootApi ) {
@@ -42,11 +39,10 @@ export function reactRender(domNode: Element, reactElement: ReactElement): Rende
42
39
return root ;
43
40
}
44
41
45
- // eslint-disable-next-line react/no-render-return-value
46
- return ReactDOM . render ( reactElement , domNode ) ;
42
+ return render ( reactElement , domNode ) ;
47
43
}
48
44
49
- export const unmountComponentAtNode : typeof ReactDOM . unmountComponentAtNode = supportsRootApi
45
+ export const unmountComponentAtNode : typeof unmountComponentAtNodeLib = supportsRootApi
50
46
? // not used if we use root API
51
47
( ) => false
52
- : ReactDOM . unmountComponentAtNode ;
48
+ : unmountComponentAtNodeLib ;
0 commit comments