Skip to content

Commit 426f597

Browse files
authored
chore: drop React createRoot warning (#296)
* test: update for warning check * chore: clean up
1 parent d2119da commit 426f597

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
"@testing-library/jest-dom": "^5.16.4",
3333
"@testing-library/react": "^13.0.0",
3434
"@types/jest": "^25.2.3",
35-
"@types/react": "^17.0.43",
36-
"@types/react-dom": "^17.0.14",
35+
"@types/react": "^18.0.0",
36+
"@types/react-dom": "^18.0.0",
3737
"@types/shallowequal": "^1.1.1",
3838
"@types/warning": "^3.0.0",
3939
"@umijs/fabric": "^2.0.8",

src/React/render.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,45 @@
11
import type * as React from 'react';
2-
import {
2+
import * as ReactDOM from 'react-dom';
3+
import type { Root } from 'react-dom/client';
4+
5+
type CreateRoot = (container: ContainerType) => Root;
6+
7+
type InternalReactDOM = typeof ReactDOM & {
8+
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED?: {
9+
usingClientEntryPoint?: boolean;
10+
};
11+
createRoot?: CreateRoot;
12+
};
13+
14+
const {
315
version,
4-
render as reactRender,
16+
render: reactRender,
517
unmountComponentAtNode,
6-
} from 'react-dom';
7-
import type { Root } from 'react-dom/client';
18+
} = ReactDOM as InternalReactDOM;
819

9-
let createRoot: (container: ContainerType) => Root;
20+
let createRoot: CreateRoot;
1021
try {
1122
const mainVersion = Number((version || '').split('.')[0]);
1223
if (mainVersion >= 18) {
13-
({ createRoot } = require('react-dom/client'));
24+
({ createRoot } = ReactDOM as InternalReactDOM);
1425
}
1526
} catch (e) {
1627
// Do nothing;
1728
}
1829

30+
function toggleWarning(skip: boolean) {
31+
const { __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED } =
32+
ReactDOM as InternalReactDOM;
33+
34+
if (
35+
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED &&
36+
typeof __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED === 'object'
37+
) {
38+
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.usingClientEntryPoint =
39+
skip;
40+
}
41+
}
42+
1943
const MARK = '__rc_react_root__';
2044

2145
// ========================== Render ==========================
@@ -24,7 +48,10 @@ type ContainerType = (Element | DocumentFragment) & {
2448
};
2549

2650
function modernRender(node: React.ReactElement, container: ContainerType) {
51+
toggleWarning(true);
2752
const root = container[MARK] || createRoot(container);
53+
toggleWarning(false);
54+
2855
root.render(node);
2956

3057
container[MARK] = root;

tests/react.test.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ describe('React', () => {
1212
});
1313

1414
it('render & unmount', async () => {
15+
const errorSpy = jest.spyOn(console, 'error');
16+
1517
const div = document.createElement('div');
1618
document.body.appendChild(div);
1719

@@ -26,6 +28,8 @@ describe('React', () => {
2628
await unmount(div);
2729
});
2830
expect(div.querySelector('.bamboo')).toBeFalsy();
31+
32+
expect(errorSpy).not.toHaveBeenCalled();
2933
});
3034

3135
it('React 17 render & unmount', async () => {

0 commit comments

Comments
 (0)