From 9f24875e20d16fa8663a1b7682ee0c4617b0a135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Thu, 16 Jan 2025 16:51:09 +0800 Subject: [PATCH] refactor: rm legacy code --- src/React/render.ts | 84 ++------------------------------------------ tests/react.test.tsx | 21 +---------- 2 files changed, 4 insertions(+), 101 deletions(-) diff --git a/src/React/render.ts b/src/React/render.ts index d7a3250f..6f2aadd6 100644 --- a/src/React/render.ts +++ b/src/React/render.ts @@ -1,43 +1,7 @@ import type * as React from 'react'; -import * as ReactDOM from 'react-dom'; +import { createRoot } from 'react-dom/client'; import type { Root } from 'react-dom/client'; -// Let compiler not to search module usage -const fullClone = { - ...ReactDOM, -} as typeof ReactDOM & { - __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED?: { - usingClientEntryPoint?: boolean; - }; - createRoot?: CreateRoot; -}; - -type CreateRoot = (container: ContainerType) => Root; - -const { version, render: reactRender, unmountComponentAtNode } = fullClone; - -let createRoot: CreateRoot; -try { - const mainVersion = Number((version || '').split('.')[0]); - if (mainVersion >= 18) { - ({ createRoot } = fullClone); - } -} catch (e) { - // Do nothing; -} - -function toggleWarning(skip: boolean) { - const { __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED } = fullClone; - - 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 ========================== @@ -45,38 +9,16 @@ type ContainerType = (Element | DocumentFragment) & { [MARK]?: Root; }; -function modernRender(node: React.ReactElement, container: ContainerType) { - toggleWarning(true); +export function render(node: React.ReactElement, container: ContainerType) { const root = container[MARK] || createRoot(container); - toggleWarning(false); 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) { +export async function unmount(container: ContainerType) { // Delay to unmount to avoid React 18 sync warning return Promise.resolve().then(() => { container[MARK]?.unmount(); @@ -84,23 +26,3 @@ async function modernUnmount(container: ContainerType) { 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); -} diff --git a/tests/react.test.tsx b/tests/react.test.tsx index db0f9899..8cbcafc5 100644 --- a/tests/react.test.tsx +++ b/tests/react.test.tsx @@ -1,7 +1,5 @@ import { act } from '@testing-library/react'; -import { _r, _u, render, unmount } from '../src/React/render'; - -globalThis.IS_REACT_ACT_ENVIRONMENT = true; +import { render, unmount } from '../src/React/render'; describe('React', () => { afterEach(() => { @@ -30,21 +28,4 @@ describe('React', () => { expect(errorSpy).not.toHaveBeenCalled(); }); - - it('React 17 render & unmount', async () => { - const div = document.createElement('div'); - document.body.appendChild(div); - - // Mount - act(() => { - _r(
, div); - }); - expect(div.querySelector('.bamboo')).toBeTruthy(); - - // Unmount - act(() => { - _u(div); - }); - expect(div.querySelector('.bamboo')).toBeFalsy(); - }); });