Skip to content

Commit 1db6001

Browse files
committed
Try to optimize react-dom imports more
1 parent 2c07f43 commit 1db6001

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

node_package/src/reactApis.cts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
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,react/no-deprecated */
2+
import { version, hydrate, render, unmountComponentAtNode as unmountComponentAtNodeLib } from 'react-dom';
33
import type { ReactElement } from 'react';
44
import type { RenderReturnType } from './types/index.ts' with { 'resolution-mode': 'import' };
55

6-
const reactMajorVersion = Number(ReactDOM.version?.split('.')[0]) || 16;
6+
const reactMajorVersion = Number(version?.split('.')[0]) || 16;
77

88
// TODO: once we require React 18, we can remove this and inline everything guarded by it.
99
export const supportsRootApi = reactMajorVersion >= 18;
1010

11-
export const supportsHydrate = supportsRootApi || 'hydrate' in ReactDOM;
11+
export const supportsHydrate = supportsRootApi || !!hydrate;
1212

1313
// TODO: once React dependency is updated to >= 18, we can remove this and just
1414
// import ReactDOM from 'react-dom/client';
@@ -20,20 +20,18 @@ if (supportsRootApi) {
2020
try {
2121
reactDomClient = require('react-dom/client') as typeof import('react-dom/client');
2222
} 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');
23+
// do nothing
2624
}
2725
}
2826

2927
type HydrateOrRenderType = (domNode: Element, reactElement: ReactElement) => RenderReturnType;
3028

31-
/* eslint-disable @typescript-eslint/no-deprecated,@typescript-eslint/no-non-null-assertion,react/no-deprecated --
29+
/* eslint-disable @typescript-eslint/no-deprecated,@typescript-eslint/no-non-null-assertion --
3230
* while we need to support React 16
3331
*/
3432
export const reactHydrate: HydrateOrRenderType = supportsRootApi
3533
? reactDomClient!.hydrateRoot
36-
: (domNode, reactElement) => ReactDOM.hydrate(reactElement, domNode);
34+
: (domNode, reactElement) => hydrate(reactElement, domNode);
3735

3836
export function reactRender(domNode: Element, reactElement: ReactElement): RenderReturnType {
3937
if (supportsRootApi) {
@@ -42,11 +40,10 @@ export function reactRender(domNode: Element, reactElement: ReactElement): Rende
4240
return root;
4341
}
4442

45-
// eslint-disable-next-line react/no-render-return-value
46-
return ReactDOM.render(reactElement, domNode);
43+
return render(reactElement, domNode);
4744
}
4845

49-
export const unmountComponentAtNode: typeof ReactDOM.unmountComponentAtNode = supportsRootApi
46+
export const unmountComponentAtNode: typeof unmountComponentAtNodeLib = supportsRootApi
5047
? // not used if we use root API
5148
() => false
52-
: ReactDOM.unmountComponentAtNode;
49+
: unmountComponentAtNodeLib;

0 commit comments

Comments
 (0)