close
Skip to content

Fix context#925

Merged
zombieJ merged 2 commits into
masterfrom
fix-context
Feb 25, 2025
Merged

Fix context#925
zombieJ merged 2 commits into
masterfrom
fix-context

Conversation

@zombieJ
Copy link
Copy Markdown
Member

@zombieJ zombieJ commented Feb 25, 2025

#905 CC -> FC 的重构把 isDisableduseMemo 包了起来。导致 UnstableContext 提供的闭包 disabled 切换会被 memo 掉。
去掉了 useMemo 还原为 CC 等效逻辑。

Summary by CodeRabbit

  • 重构
    • 优化了树组件禁用状态的计算方式,改为直接判断,确保每次渲染时状态更新准确。
  • 测试
    • 增加了新的测试用例,以验证在动态场景下树节点禁用状态的正确切换。

@vercel
Copy link
Copy Markdown

vercel Bot commented Feb 25, 2025

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

Name Status Preview Comments Updated (UTC)
tree ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 25, 2025 8:49am

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Feb 25, 2025

Walkthrough

本次变更主要修改了 TreeNode.tsx 中计算 isDisabled 状态的方式,将原先基于 React.useMemo 的缓存计算改为每次直接计算,同时保持对上下文中 disabledunstableContext.nodeDisabled 以及组件 props 的判断。新增的测试用例则验证了在上下文变更时节点禁用样式的动态更新。

Changes

文件 变更总结
src/TreeNode.tsx 移除了 React.useMemo,改为直接根据 context 与 props 计算 isDisabled 状态。
tests/Tree.spec.tsx 新增测试用例 “correct handle closure”,验证 unstableContext.nodeDisabled 对树节点禁用状态更新的动态影响。

Sequence Diagram(s)

sequenceDiagram
    participant T as 测试用例
    participant Tree as 树组件
    participant Node as TreeNode
    participant Ctx as UnstableContext

    T->>Tree: 初始渲染 (nodeDisabled = false)
    Tree->>Node: 根据 context 与 props 计算 isDisabled
    Node->>Tree: 渲染启用状态节点

    T->>Ctx: 修改 nodeDisabled 为 true
    T->>Tree: 重新渲染组件
    Tree->>Node: 重新计算 isDisabled (更新后的 context)
    Node->>Tree: 渲染禁用状态节点 (添加 rc-tree-treenode-disabled 类)
Loading

Possibly related PRs

Suggested reviewers

  • MadCcc
  • afc163

Poem

嗨,我是一只快乐的小兔子,
在代码森林中蹦跶不停步。
缓存离去,计算即时照亮路径,
测试响起,验证每段代码的独特风情。
代码与我共舞,未来由此熠熠生辉!
🐰🌟

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

src/TreeNode.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-react".

(The package "eslint-plugin-react" was not found when loaded as a Node module from the directory "".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-react@latest --save-dev

The plugin "eslint-plugin-react" was referenced from the config file in ".eslintrc.js » /node_modules/.pnpm/@umijs+fabric@4.0.1_jest@29.7.0_@types+node@22.13.5__postcss@8.5.3/node_modules/@umijs/fabric/dist/eslint.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

tests/Tree.spec.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-react".

(The package "eslint-plugin-react" was not found when loaded as a Node module from the directory "".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-react@latest --save-dev

The plugin "eslint-plugin-react" was referenced from the config file in ".eslintrc.js » /node_modules/.pnpm/@umijs+fabric@4.0.1_jest@29.7.0_@types+node@22.13.5__postcss@8.5.3/node_modules/@umijs/fabric/dist/eslint.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c157a57 and 249c748.

📒 Files selected for processing (2)
  • src/TreeNode.tsx (1 hunks)
  • tests/Tree.spec.tsx (1 hunks)
🔇 Additional comments (2)
src/TreeNode.tsx (1)

50-50: 将 isDisabled 从 useMemo 中移出,修复上下文更新问题

这一改动把 isDisabled 的计算从 useMemo 中移出,改为直接计算。这样可以确保每次渲染时都能获取最新的 unstableContext.nodeDisabled?.(data) 结果,避免因闭包捕获的值过时而导致的更新问题。这与 PR 描述中提到的问题相符。

tests/Tree.spec.tsx (1)

1315-1342: 添加闭包处理测试确保 nodeDisabled 的状态能正确更新

这个测试用例验证了当 nodeDisabled 函数依赖的外部变量 disabled 发生变化时,节点的禁用状态能够正确更新。测试中:

  1. 初始设置 disabled = false 并验证节点未被禁用
  2. 修改 disabled = true 后重新渲染,验证节点正确显示为禁用状态

这个测试直接验证了在 TreeNode 中移除 useMemo 后的行为是否符合预期,确保上下文变更时节点禁用状态能够动态更新。

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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.

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.84%. Comparing base (c157a57) to head (249c748).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #925      +/-   ##
==========================================
- Coverage   99.84%   99.84%   -0.01%     
==========================================
  Files          14       14              
  Lines        1275     1274       -1     
  Branches      383      389       +6     
==========================================
- Hits         1273     1272       -1     
  Misses          2        2              

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

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.

1 participant