From b78fa7c17b8b58809a5e5acca58762498f646b15 Mon Sep 17 00:00:00 2001 From: zombiej Date: Tue, 12 Apr 2022 17:23:32 +0800 Subject: [PATCH 1/2] test: update for warning check --- package.json | 4 ++-- src/React/render.ts | 44 ++++++++++++++++++++++++++++++++++++++------ tests/react.test.tsx | 4 ++++ 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index fcf55298..91d059fd 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/React/render.ts b/src/React/render.ts index 05f74e3d..d0dbc34a 100644 --- a/src/React/render.ts +++ b/src/React/render.ts @@ -1,21 +1,50 @@ 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); + + // const { __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED } = + // ReactDOM as any; + // __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.usingClientEntryPoint = + // true; } } 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 ========================== @@ -24,7 +53,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; diff --git a/tests/react.test.tsx b/tests/react.test.tsx index 1df19531..cd1d9a77 100644 --- a/tests/react.test.tsx +++ b/tests/react.test.tsx @@ -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); @@ -26,6 +28,8 @@ describe('React', () => { await unmount(div); }); expect(div.querySelector('.bamboo')).toBeFalsy(); + + expect(errorSpy).not.toHaveBeenCalled(); }); it('React 17 render & unmount', async () => { From 4bd23642531fbffc26b1386f6d9201fc4cae07d2 Mon Sep 17 00:00:00 2001 From: zombiej Date: Tue, 12 Apr 2022 17:35:12 +0800 Subject: [PATCH 2/2] chore: clean up --- src/React/render.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/React/render.ts b/src/React/render.ts index d0dbc34a..34a749d3 100644 --- a/src/React/render.ts +++ b/src/React/render.ts @@ -22,11 +22,6 @@ try { const mainVersion = Number((version || '').split('.')[0]); if (mainVersion >= 18) { ({ createRoot } = ReactDOM as InternalReactDOM); - - // const { __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED } = - // ReactDOM as any; - // __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.usingClientEntryPoint = - // true; } } catch (e) { // Do nothing;