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

fix: trigger only when changed #299

Merged
merged 1 commit into from
Jan 16, 2025
Merged

fix: trigger only when changed #299

merged 1 commit into from
Jan 16, 2025

Conversation

zombieJ
Copy link
Member

@zombieJ zombieJ commented Jan 16, 2025

加了一个阻断,只有在统计出现变化的时候才触发重新渲染。
比较神奇,只有特定情况下才会产生回荡,测试模拟不出来这个情况。

fix ant-design/ant-design#52400

Summary by CodeRabbit

  • 性能优化
    • 优化了高度收集逻辑,仅在实际高度发生变化时触发更新。
    • 改进了状态更新机制,减少不必要的重新渲染。

Copy link

vercel bot commented Jan 16, 2025

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

Name Status Preview Comments Updated (UTC)
virtual-list ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 16, 2025 7:02am

Copy link

coderabbitai bot commented Jan 16, 2025

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/hooks/useHeights.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

Error: Cannot read config file: /.eslintrc.js
Error: Cannot find module '@umijs/fabric/dist/eslint'
Require stack:

  • /.eslintrc.js
  • /node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/lib/cli-engine/cli-engine.js
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/lib/eslint/eslint.js
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/lib/eslint/index.js
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/lib/cli.js
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/bin/eslint.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1248:15)
    at Module._load (node:internal/modules/cjs/loader:1074:27)
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)
    at Module.require (node:internal/modules/cjs/loader:1339:12)
    at require (node:internal/modules/helpers:135:16)
    at Object. (/.eslintrc.js:1:14)
    at Module._compile (node:internal/modules/cjs/loader:1546:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1691:10)
    at Module.load (node:internal/modules/cjs/loader:1317:32)

变更概览

详细介绍

src/hooks/useHeights.tsx 中,引入了一个新的变量 changed,用于跟踪高度值是否发生变化。在 collectHeight 函数中,现在会比较每个元素的高度是否与 heightsRef 中存储的先前值不同。如果检测到变化,changed 变量将被设置为 true。更新 setUpdatedMark 的逻辑现在仅在 changedtrue 时才递增 updatedMark 状态,从而避免在没有实际高度变化时进行不必要的更新。

变更

文件 变更摘要
src/hooks/useHeights.tsx 添加 changed 变量,优化高度变化检测逻辑

对链接问题的评估

目标 是否解决 说明
✅ 解决页面崩溃问题 [#52400] -

可能相关的 PR

兔子庆祝诗

高度变化,悄无声息,
兔子跳跃,代码精巧,
性能优化,如丝般柔,
无效更新,一去不回,
虚拟列表,轻盈飞翔!🐰✨


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

codecov bot commented Jan 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.50%. Comparing base (10ca51d) to head (3cc2f50).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #299   +/-   ##
=======================================
  Coverage   97.49%   97.50%           
=======================================
  Files          19       19           
  Lines         799      802    +3     
  Branches      190      187    -3     
=======================================
+ Hits          779      782    +3     
  Misses         20       20           

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

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)
src/hooks/useHeights.tsx (1)

Line range hint 36-58: 整体架构建议

这个优化方案实现得很优雅,完全符合 React 的性能优化原则。建议考虑以下几点来进一步加强这个实现:

  1. 可以考虑添加一个 debug 模式,在开发环境打印出高度变化的详细信息
  2. 建议在组件的文档中说明这个优化机制,方便其他开发者理解
  3. 可以考虑添加一个阈值配置,只有当高度变化超过特定值时才触发更新
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 10ca51d and 3cc2f50.

📒 Files selected for processing (1)
  • src/hooks/useHeights.tsx (2 hunks)
🔇 Additional comments (3)
src/hooks/useHeights.tsx (3)

36-37: 代码逻辑清晰,变量命名恰当!

引入 changed 标志位来追踪高度变化是一个很好的优化方案。


50-50: 标记变更状态的位置准确!

在检测到实际高度变化时才设置 changed 标志,避免了不必要的更新。


56-58: 优化方案有效,建议验证边缘情况!

通过 changed 条件判断来控制更新是个很好的优化,能有效防止不必要的重渲染。不过建议验证一下以下场景:

  1. 在快速滚动时是否会影响性能
  2. 在动态改变内容高度时是否能正确触发更新
✅ Verification successful

优化验证结果:实现安全可靠!

经过源码分析,该优化方案在以下方面都有保障:

  1. 通过 ResizeObserver 可靠监测高度变化
  2. 与滚动事件处理良好集成
  3. 已有测试用例覆盖相关场景

无需担心边缘情况,代码结构完善。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 搜索可能受此更改影响的滚动和动态高度相关代码
rg -l 'scroll|height|resize' --type tsx --type ts

Length of output: 79


Script:

#!/bin/bash
# 搜索高度计算和滚动相关的代码
rg "useHeights|scrollHeight|clientHeight|ResizeObserver|onScroll" 

# 查找可能使用了useHeights的组件
ast-grep --pattern 'useHeights($$$)'

Length of output: 4386

@zombieJ zombieJ merged commit 5d16ba8 into master Jan 16, 2025
12 checks passed
@zombieJ zombieJ deleted the fix-loop branch January 16, 2025 07:19
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.17.0版本的rc-virtuial-list配合antd的checkbox使用时,会导致页面崩溃
1 participant