Skip to content
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

feat: 支持同时在 React 16/17/18 下运行 #2802

Merged
merged 3 commits into from
Jul 5, 2024

Conversation

lijinke666
Copy link
Member

@lijinke666 lijinke666 commented Jul 3, 2024

👀 PR includes

✨ Feature

  • New feature

📝 Description

原本 Tooltip 做了 React 16/18 的兼容, 但在实际运行时 react-dom/client 的方式打包工具会报错

image

本质上 react-dom/clientreact-dom 的一个 sub module

https://github.com/facebook/react/blob/main/packages/react-dom/package.json#L51-L59

所以参考 antd 的处理方式, 使用 import * 的方式全部引入, 然后做一个克隆, 跟进版本动态判断是否使用 createRoot

https://github.com/react-component/util/blob/677d3ac177d147572b65af63e67a7796a5104f4c/src/React/render.ts#L6-L13

import * as ReactDOM from 'react-dom';
import type { Root } from 'react-dom/client';

const ReactDOMClone = {
  ...ReactDOM,
} as typeof ReactDOM & {
  __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED?: {
    usingClientEntryPoint?: boolean;
  };
  createRoot?: CreateRoot;
};

其他:

React 19 已发布 RC 版本, 社区负面反馈较多, 后续兼容视React 19 稳定情况而定。

🖼️ Screenshot

Before After

🔗 Related issue link

🔍 Self-Check before the merge

  • Add or update relevant docs.
  • Add or update relevant demos.
  • Add or update test case.
  • Add or update relevant TypeScript definitions.

Copy link

vercel bot commented Jul 3, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
antvis-s2 ❌ Failed (Inspect) Jul 3, 2024 7:48am

@github-actions github-actions bot added pr(feature) new feature next 2.0-next 版本的问题 labels Jul 3, 2024
Copy link
Contributor

github-actions bot commented Jul 3, 2024

Size Change: +67 B (+0.02%)

Total Size: 326 kB

Filename Size Change
./packages/s2-react/dist/index.min.js 69.9 kB +67 B (+0.1%)
ℹ️ View Unchanged
Filename Size
./packages/s2-core/dist/index.min.js 228 kB
./packages/s2-core/dist/style.min.css 388 B
./packages/s2-react/dist/style.min.css 3.88 kB
./packages/s2-vue/dist/index.min.js 22 kB
./packages/s2-vue/dist/style.min.css 1.97 kB

compressed-size-action

@codecov-commenter
Copy link

codecov-commenter commented Jul 3, 2024

Codecov Report

Attention: Patch coverage is 75.71429% with 17 lines in your changes missing coverage. Please review.

Project coverage is 80.23%. Comparing base (ecdc9c8) to head (8dfb7d6).
Report is 398 commits behind head on next.

Files Patch % Lines
packages/s2-react/src/utils/reactRender.ts 83.33% 5 Missing and 5 partials ⚠️
packages/s2-react/src/utils/invokeComponent.tsx 0.00% 6 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             next    #2802      +/-   ##
==========================================
+ Coverage   75.77%   80.23%   +4.46%     
==========================================
  Files         257      181      -76     
  Lines       11994    10384    -1610     
  Branches     2464     2344     -120     
==========================================
- Hits         9088     8332     -756     
+ Misses       1398      607     -791     
+ Partials     1508     1445      -63     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

let createRoot: CreateRoot;

try {
const mainVersion = Number((version || '').split('.')[0]);
Copy link
Member Author

Choose a reason for hiding this comment

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

参考 antd 的 react-component/util 处理, 做一个克隆, 动态生成 createRoot

*/
import { version } from 'react';
import * as ReactDOM from 'react-dom';
import type { Root } from 'react-dom/client';
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 type 不受影响

jest.mock('react', () => {
return {
...jest.requireActual('react'),
version: '16.9.0',
Copy link
Member Author

Choose a reason for hiding this comment

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

判断不同版本的 renderunmount 执行分支

@lijinke666 lijinke666 merged commit 84253cb into next Jul 5, 2024
8 of 9 checks passed
@lijinke666 lijinke666 deleted the feat-react-16-compatibility branch July 5, 2024 08:02
@lijinke666
Copy link
Member Author

🎉 This PR is included in version @antv/s2-react-v2.0.0-next.24 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
next 2.0-next 版本的问题 pr(feature) new feature released on @next
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants