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

remove React.findDOMNode #611

Merged
merged 4 commits into from
Jan 10, 2025
Merged

remove React.findDOMNode #611

merged 4 commits into from
Jan 10, 2025

Conversation

Zyf665
Copy link
Contributor

@Zyf665 Zyf665 commented Dec 26, 2024

remove React.findDOMNode

Summary by CodeRabbit

  • 新特性

    • 更新了 findDOMNode 函数,支持新的类型 { current: T },并可能返回 null
    • 测试用例中添加了新的私有属性 elementRef,以增强组件对其 DOM 节点的识别能力。
    • 新增测试用例,验证从 ref.current 返回 DOM 节点的正确性。
    • 新增测试用例,检查未挂载的 ref 返回 null 的情况。
  • 错误修复

    • 修改了 DOMWrapper 类的测试期望,从期望错误更改为不期望错误。

Copy link

vercel bot commented Dec 26, 2024

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

Name Status Preview Comments Updated (UTC)
util ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 8, 2025 1:01pm

Copy link

coderabbitai bot commented Dec 26, 2024

概述

遍历

src/Dom/findDOMNode.ts 文件中,findDOMNode 函数的参数和返回类型已被修改。新的实现支持接受带有 current 属性的引用对象,并且现在可以返回 null。函数的内部逻辑已更新,以处理不同类型的节点引用。

变更

文件 变更摘要
src/Dom/findDOMNode.ts - 函数签名更新:支持 { current: T } 类型
- 返回类型变为 `T
tests/findDOMNode.test.tsx - 在 DOMWrapper 类中添加 elementRef
- 更新 getDOM 方法
- 修改渲染方法以附加引用
- 添加新测试用例:验证从 ref.current 返回 DOM 节点
- 添加新测试用例:验证未挂载时返回 null

序列图

sequenceDiagram
    participant Caller
    participant findDOMNode
    participant Ref
    
    Caller->>findDOMNode: 传入节点/引用
    alt 引用有 current 属性
        findDOMNode->>Ref: 获取 current
        Ref-->>findDOMNode: 返回 DOM 节点
    else 其他类型节点
        findDOMNode-->>Caller: 返回相应的 DOM 节点或 null
    end
Loading

诗歌

🐰 代码跳跃,引用飞舞
DOM 节点寻踪,灵活多变
Null 与类型,舞动精华
重构之路,兔子欢唱
技术之美,悄然绽放 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
tests/findDOMNode.test.tsx (2)

40-40: 调用 findDOMNode(this.elementRef) 时请确保传入的是正确的 ref 对象
此处直接将 elementRef 传给 findDOMNode,若后续需要在 elementRef 中放置其他类型的节点或者逻辑,请注意和 findDOMNode 的类型签名保持一致。


44-44: 注意为 <div> 提供更多可读性或可访问性属性
可以考虑在 <div> 标签中添加一些标识符属性(例如 data-testid)以便测试和调试时更好地定位元素。

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fab86ea and 3b03baf.

📒 Files selected for processing (2)
  • src/Dom/findDOMNode.ts (1 hunks)
  • tests/findDOMNode.test.tsx (2 hunks)
🔇 Additional comments (4)
src/Dom/findDOMNode.ts (2)

28-34: 使用联合类型后返回值可能为 null 的注意事项
函数返回值从 T 更新为 T | null。在调用处要注意判空逻辑,避免在返回值为 null 时出现潜在错误。

请确认所有函数调用方都能够正确处理返回值为 null 的情况,如果需要,我可以提供一段脚本来搜索所有调用点并检查是否进行判空处理。


40-41: 通过判断 current 属性获取 DOM 的实现已替换 React.findDOMNode
这段逻辑去除了对 React.findDOMNode 的依赖,改为直接使用 current 属性获取 DOM。实现简单且更符合 React 16+ 推荐的使用方式。

tests/findDOMNode.test.tsx (2)

38-38: 新增 elementRef 属性以替代旧的 findDOMNode 使用场景
通过在类组件内部定义 elementRef 来获取 DOM,大幅降低对 findDOMNode 的依赖。此做法符合官方最佳实践,值得推广。


57-57: 确认不会触发旧式警告或 React 内部提示
此处针对 console.error 的断言表明渲染此组件不再引发警告,证明移除 React.findDOMNode 后没有产生副作用。

const domNode = getDOM(node);
if (domNode) {
return domNode as T;
}

if (node instanceof React.Component) {
return ReactDOM.findDOMNode?.(node) as unknown as T;
if (node && typeof node === 'object' && 'current' in node) {
Copy link
Member

Choose a reason for hiding this comment

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

还要检测一下这个 current 是不是 DOM

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的

const domNode = getDOM(node);
if (domNode) {
return domNode as T;
}

if (node instanceof React.Component) {
return ReactDOM.findDOMNode?.(node) as unknown as T;
if (node && typeof node === 'object' && 'current' in node && isDOM(node.current)) {
Copy link
Member

Choose a reason for hiding this comment

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

这对于 nativeElement 还是没用的,应该是:

if (node && typeof node === 'object' && 'current' in node) {
  const refDomNode = getDOM(node.current);
  if (refDomNode) {
    return refDomNode as T;
  }
}

Copy link
Member

Choose a reason for hiding this comment

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

另外,这个新的写法需要配对一下测试~

Copy link
Contributor Author

Choose a reason for hiding this comment

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

佬 我看 nativeElement 有一个测试用例了 我这边还需要再加一个吗

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
tests/findDOMNode.test.tsx (1)

94-98: 建议增强未挂载引用的测试用例

当前测试验证了基本场景,建议添加以下测试场景:

  • 组件卸载后的行为
  • 异步更新场景
  • 多次挂载/卸载循环

建议添加如下测试代码:

it('should handle unmount scenarios', () => {
  const elementRef = React.createRef<HTMLDivElement>();
  const { unmount } = render(
    <div ref={elementRef}>test</div>
  );
  
  expect(findDOMNode(elementRef)).not.toBeNull();
  unmount();
  expect(findDOMNode(elementRef)).toBeNull();
});
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d89ec1a and 79d037b.

📒 Files selected for processing (2)
  • src/Dom/findDOMNode.ts (1 hunks)
  • tests/findDOMNode.test.tsx (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/Dom/findDOMNode.ts
🔇 Additional comments (3)
tests/findDOMNode.test.tsx (3)

38-44: 代码改动符合最佳实践!

使用 React.createRef 替代 findDOMNode 是正确的改进方向,这样可以:

  • 避免使用已废弃的 findDOMNode API
  • 提供更好的类型安全性
  • 符合 React 的声明式理念

57-57: 测试期望更新正确!

由于现在使用了 React.createRef 而不是 findDOMNode,确实不应该触发任何警告,测试用例的期望值更新是合理的。


79-92: 测试用例设计合理!

新增的测试用例很好地验证了:

  • 使用 forwardRef 转发引用
  • 正确获取 DOM 节点
  • 符合 React 的最新实践

Copy link

codecov bot commented Jan 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.15%. Comparing base (26a5fe2) to head (79d037b).
Report is 10 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #611      +/-   ##
==========================================
+ Coverage   89.90%   90.15%   +0.24%     
==========================================
  Files          41       36       -5     
  Lines         991      914      -77     
  Branches      320      300      -20     
==========================================
- Hits          891      824      -67     
+ Misses         97       88       -9     
+ Partials        3        2       -1     

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

@zombieJ zombieJ merged commit b3b65e6 into react-component:master Jan 10, 2025
10 checks passed
it('should return DOM node from ref.current', () => {
const TestComponent = React.forwardRef<HTMLDivElement>((_, ref) => {
return <div ref={ref}>test</div>;
});
Copy link
Member

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants