Skip to content

Commit 6e2b215

Browse files
authored
refactor: rm legacy code (#617)
1 parent 4662158 commit 6e2b215

File tree

2 files changed

+4
-101
lines changed

2 files changed

+4
-101
lines changed

src/React/render.ts

Lines changed: 3 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,28 @@
11
import type * as React from 'react';
2-
import * as ReactDOM from 'react-dom';
2+
import { createRoot } from 'react-dom/client';
33
import type { Root } from 'react-dom/client';
44

5-
// Let compiler not to search module usage
6-
const fullClone = {
7-
...ReactDOM,
8-
} as typeof ReactDOM & {
9-
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED?: {
10-
usingClientEntryPoint?: boolean;
11-
};
12-
createRoot?: CreateRoot;
13-
};
14-
15-
type CreateRoot = (container: ContainerType) => Root;
16-
17-
const { version, render: reactRender, unmountComponentAtNode } = fullClone;
18-
19-
let createRoot: CreateRoot;
20-
try {
21-
const mainVersion = Number((version || '').split('.')[0]);
22-
if (mainVersion >= 18) {
23-
({ createRoot } = fullClone);
24-
}
25-
} catch (e) {
26-
// Do nothing;
27-
}
28-
29-
function toggleWarning(skip: boolean) {
30-
const { __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED } = fullClone;
31-
32-
if (
33-
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED &&
34-
typeof __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED === 'object'
35-
) {
36-
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.usingClientEntryPoint =
37-
skip;
38-
}
39-
}
40-
415
const MARK = '__rc_react_root__';
426

437
// ========================== Render ==========================
448
type ContainerType = (Element | DocumentFragment) & {
459
[MARK]?: Root;
4610
};
4711

48-
function modernRender(node: React.ReactElement, container: ContainerType) {
49-
toggleWarning(true);
12+
export function render(node: React.ReactElement, container: ContainerType) {
5013
const root = container[MARK] || createRoot(container);
51-
toggleWarning(false);
5214

5315
root.render(node);
5416

5517
container[MARK] = root;
5618
}
5719

58-
function legacyRender(node: React.ReactElement, container: ContainerType) {
59-
reactRender?.(node, container);
60-
}
61-
62-
/** @private Test usage. Not work in prod */
63-
export function _r(node: React.ReactElement, container: ContainerType) {
64-
if (process.env.NODE_ENV !== 'production') {
65-
return legacyRender(node, container);
66-
}
67-
}
68-
69-
export function render(node: React.ReactElement, container: ContainerType) {
70-
if (createRoot) {
71-
modernRender(node, container);
72-
return;
73-
}
74-
75-
legacyRender(node, container);
76-
}
77-
7820
// ========================= Unmount ==========================
79-
async function modernUnmount(container: ContainerType) {
21+
export async function unmount(container: ContainerType) {
8022
// Delay to unmount to avoid React 18 sync warning
8123
return Promise.resolve().then(() => {
8224
container[MARK]?.unmount();
8325

8426
delete container[MARK];
8527
});
8628
}
87-
88-
function legacyUnmount(container: ContainerType) {
89-
unmountComponentAtNode(container);
90-
}
91-
92-
/** @private Test usage. Not work in prod */
93-
export function _u(container: ContainerType) {
94-
if (process.env.NODE_ENV !== 'production') {
95-
return legacyUnmount(container);
96-
}
97-
}
98-
99-
export async function unmount(container: ContainerType) {
100-
if (createRoot !== undefined) {
101-
// Delay to unmount to avoid React 18 sync warning
102-
return modernUnmount(container);
103-
}
104-
105-
legacyUnmount(container);
106-
}

tests/react.test.tsx

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { act } from '@testing-library/react';
2-
import { _r, _u, render, unmount } from '../src/React/render';
3-
4-
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
2+
import { render, unmount } from '../src/React/render';
53

64
describe('React', () => {
75
afterEach(() => {
@@ -30,21 +28,4 @@ describe('React', () => {
3028

3129
expect(errorSpy).not.toHaveBeenCalled();
3230
});
33-
34-
it('React 17 render & unmount', async () => {
35-
const div = document.createElement('div');
36-
document.body.appendChild(div);
37-
38-
// Mount
39-
act(() => {
40-
_r(<div className="bamboo" />, div);
41-
});
42-
expect(div.querySelector('.bamboo')).toBeTruthy();
43-
44-
// Unmount
45-
act(() => {
46-
_u(div);
47-
});
48-
expect(div.querySelector('.bamboo')).toBeFalsy();
49-
});
5031
});

0 commit comments

Comments
 (0)