Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.0.0",
"@types/jest": "^25.2.3",
"@types/react": "^17.0.43",
"@types/react-dom": "^17.0.14",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@types/shallowequal": "^1.1.1",
"@types/warning": "^3.0.0",
"@umijs/fabric": "^2.0.8",
Expand Down
39 changes: 33 additions & 6 deletions src/React/render.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
import type * as React from 'react';
import {
import * as ReactDOM from 'react-dom';
import type { Root } from 'react-dom/client';

type CreateRoot = (container: ContainerType) => Root;

type InternalReactDOM = typeof ReactDOM & {
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED?: {
usingClientEntryPoint?: boolean;
};
createRoot?: CreateRoot;
};

const {
version,
render as reactRender,
render: reactRender,
unmountComponentAtNode,
} from 'react-dom';
import type { Root } from 'react-dom/client';
} = ReactDOM as InternalReactDOM;

let createRoot: (container: ContainerType) => Root;
let createRoot: CreateRoot;
try {
const mainVersion = Number((version || '').split('.')[0]);
if (mainVersion >= 18) {
({ createRoot } = require('react-dom/client'));
({ createRoot } = ReactDOM as InternalReactDOM);
}
} catch (e) {
// Do nothing;
}

function toggleWarning(skip: boolean) {
const { __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED } =
ReactDOM as InternalReactDOM;

if (
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED &&
typeof __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED === 'object'
) {
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.usingClientEntryPoint =
skip;
}
}

const MARK = '__rc_react_root__';

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

function modernRender(node: React.ReactElement, container: ContainerType) {
toggleWarning(true);
const root = container[MARK] || createRoot(container);
toggleWarning(false);

root.render(node);

container[MARK] = root;
Expand Down
4 changes: 4 additions & 0 deletions tests/react.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ describe('React', () => {
});

it('render & unmount', async () => {
const errorSpy = jest.spyOn(console, 'error');

const div = document.createElement('div');
document.body.appendChild(div);

Expand All @@ -26,6 +28,8 @@ describe('React', () => {
await unmount(div);
});
expect(div.querySelector('.bamboo')).toBeFalsy();

expect(errorSpy).not.toHaveBeenCalled();
});

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