-
Couldn't load subscription status.
- Fork 191
feat: add React/render util #292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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')); | ||
| } | ||
| } catch (e) { | ||
| // Do nothing; | ||
| } | ||
|
|
||
| const MARK = '__rc_react_root__'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 用个 Symbol? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个 require 会不会对工具链有要求?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
比如 webpack 和 vite。
There was a problem hiding this comment.
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
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
干脆直接用 ReactDOM 里的 createRoot 好了
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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: