Skip to content

fix(Indent): Used deep check for props equality in Indent component #961

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mfabek
Copy link

@mfabek mfabek commented Jul 3, 2025

  • Previously, shallow equality was used, which ended up rerendering Indent even if props were the same
  • The issue was with isStart and isEnd arrays, which were only reference checked, but they should be checked deeply
  • In the image below, I am using why-did-you-render, which ended up logging bunch of logs related to the Indent component

Screenshot 2025-07-03 at 15 40 42

Summary by CodeRabbit

  • 优化
    • 提升了 Indent 组件的性能,减少不必要的重新渲染,提高了界面响应速度。

Previously shallow equality was used which ended up rerendering Indent even if props were the same
Copy link

coderabbitai bot commented Jul 3, 2025

Walkthrough

此次更改在 React 组件 Indent 的 memoization 机制中引入了自定义比较函数 arePropsEqual,以实现对布尔数组 isStartisEnd 的深度比较。为此,新增了工具函数 areArraysEqual,用于判断两个数组内容是否完全一致。

Changes

文件/分组 变更摘要
src/Indent.tsx 添加 arePropsEqual 比较函数,Indent 组件使用自定义 memo 比较器
src/utils/diffUtil.ts 新增 areArraysEqual 数组深度比较工具函数

Poem

🐰
小兔挥爪写新篇,
数组深比不再浅。
Indent 组件更聪明,
只为真变才重现。
工具函数来助力,
代码清晰又安全!

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/Indent.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@30.0.4_@types+node@24.0.10__postcss@8.5.6/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.

src/utils/diffUtil.ts

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@30.0.4_@types+node@24.0.10__postcss@8.5.6/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.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Post Copyable Unit Tests in a Comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @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

vercel bot commented Jul 3, 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 Jul 4, 2025 2:32am

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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a45cf7b and e2f2076.

📒 Files selected for processing (2)
  • src/Indent.tsx (2 hunks)
  • src/utils/diffUtil.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/Indent.tsx (1)
src/utils/diffUtil.ts (1)
  • areArraysEqual (47-49)
🔇 Additional comments (3)
src/Indent.tsx (3)

3-3: 导入语句正确

正确导入了新的工具函数,支持深度数组比较功能。


34-41: 自定义比较函数实现得当

arePropsEqual 函数正确实现了所有属性的比较:

  • 对基本类型属性使用浅比较(prefixCls, level
  • 对数组属性使用深度比较(isStart, isEnd

这个实现完全符合 PR 目标,能够有效防止由于数组引用变化但内容相同导致的不必要重渲染。


43-43: React.memo 使用正确

正确地将自定义比较函数传递给 React.memo,这将解决原始问题中由于浅比较导致的性能问题。

Comment on lines +47 to +49
export function areArraysEqual(a: any[], b: any[]) {
return a.length === b.length && a.every((val, i) => val === b[i]);
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

建议改进类型安全性和边界情况处理

当前实现存在以下问题:

  1. 使用 any[] 类型降低了类型安全性
  2. 缺乏对 nullundefined 的处理,可能导致运行时错误

建议应用以下改进:

-export function areArraysEqual(a: any[], b: any[]) {
-  return a.length === b.length && a.every((val, i) => val === b[i]);
+export function areArraysEqual<T>(a: T[], b: T[]): boolean {
+  if (a === b) return true;
+  if (!a || !b) return false;
+  return a.length === b.length && a.every((val, i) => val === b[i]);

这样可以:

  • 提供更好的类型安全性
  • 处理相同引用的快速路径
  • 安全处理 null/undefined 情况
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export function areArraysEqual(a: any[], b: any[]) {
return a.length === b.length && a.every((val, i) => val === b[i]);
}
export function areArraysEqual<T>(a: T[], b: T[]): boolean {
if (a === b) return true;
if (!a || !b) return false;
return a.length === b.length && a.every((val, i) => val === b[i]);
}
🤖 Prompt for AI Agents
In src/utils/diffUtil.ts around lines 47 to 49, the function areArraysEqual uses
any[] which reduces type safety and does not handle null or undefined inputs,
risking runtime errors. Update the function to use generic types for better type
safety, add a quick check for reference equality to optimize performance, and
include explicit checks to safely handle cases where either input is null or
undefined before proceeding with element-wise comparison.

@afc163
Copy link
Member

afc163 commented Jul 4, 2025

图片

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.

2 participants