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": "^16.9.3",
"@types/react-dom": "^16.9.1",
"@types/react": "^17.0.43",
"@types/react-dom": "^17.0.14",
"@types/shallowequal": "^1.1.1",
"@types/warning": "^3.0.0",
"@umijs/fabric": "^2.0.8",
Expand Down
81 changes: 81 additions & 0 deletions src/React/render.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import type * as React from 'react';
import {
version,
render as reactRender,
unmountComponentAtNode,
} from 'react-dom';
import type { Root } from 'react-dom/client';

let createRoot: (container: ContainerType) => Root;
try {
const mainVersion = Number((version || '').split('.')[0]);
if (mainVersion >= 18) {
({ createRoot } = require('react-dom/client'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 require 会不会对工具链有要求?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

比如 webpack 和 vite。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有,不过 import 本身也是需要工具链来处理的。转换后其实其实 require:https://unpkg.com/browse/antd@4.19.5/lib/affix/index.js

Copy link
Member

@afc163 afc163 Apr 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

干脆直接用 ReactDOM 里的 createRoot 好了

Copy link
Member Author

@zombieJ zombieJ Apr 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReactDOM 的 createRoot 会在 console 打 warning,让你用 react-dom/client 的 createRoot:

Warning: You are importing createRoot from "react-dom" which is not supported. You should instead import it from "react-dom/client". react-dom.development.js:73

}
} catch (e) {
// Do nothing;
}

const MARK = '__rc_react_root__';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用个 Symbol?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Symbol IE 不兼容


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

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

container[MARK] = root;
}

function legacyRender(node: React.ReactElement, container: ContainerType) {
reactRender(node, container);
}

/** @private Test usage. Not work in prod */
export function _r(node: React.ReactElement, container: ContainerType) {
if (process.env.NODE_ENV !== 'production') {
return legacyRender(node, container);
}
}

export function render(node: React.ReactElement, container: ContainerType) {
if (createRoot) {
modernRender(node, container);
return;
}

legacyRender(node, container);
}

// ========================= Unmount ==========================
async function modernUnmount(container: ContainerType) {
// Delay to unmount to avoid React 18 sync warning
return Promise.resolve().then(() => {
container[MARK]?.unmount();

delete container[MARK];
});
}

function legacyUnmount(container: ContainerType) {
unmountComponentAtNode(container);
}

/** @private Test usage. Not work in prod */
export function _u(container: ContainerType) {
if (process.env.NODE_ENV !== 'production') {
return legacyUnmount(container);
}
}

export async function unmount(container: ContainerType) {
if (createRoot !== undefined) {
// Delay to unmount to avoid React 18 sync warning
return modernUnmount(container);
}

legacyUnmount(container);
}
47 changes: 47 additions & 0 deletions tests/react.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React from 'react';
import { act } from 'react-dom/test-utils';
import { render, unmount, _r, _u } from '../src/React/render';

globalThis.IS_REACT_ACT_ENVIRONMENT = true;

describe('React', () => {
afterEach(() => {
Array.from(document.body.childNodes).forEach(node => {
document.body.removeChild(node);
});
});

it('render & unmount', async () => {
const div = document.createElement('div');
document.body.appendChild(div);

// Mount
act(() => {
render(<div className="bamboo" />, div);
});
expect(div.querySelector('.bamboo')).toBeTruthy();

// Unmount
await act(async () => {
await unmount(div);
});
expect(div.querySelector('.bamboo')).toBeFalsy();
});

it('React 17 render & unmount', async () => {
const div = document.createElement('div');
document.body.appendChild(div);

// Mount
act(() => {
_r(<div className="bamboo" />, div);
});
expect(div.querySelector('.bamboo')).toBeTruthy();

// Unmount
act(() => {
_u(div);
});
expect(div.querySelector('.bamboo')).toBeFalsy();
});
});