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: notification memory leak #361

Merged

Conversation

zhourengui
Copy link
Contributor

@zhourengui zhourengui commented Sep 16, 2024

Description

当使用stack时 notification会造成内存泄漏

Visuals

修改前的dom节点:
image

修改后的dom节点:
image

Checklist

  • My code follows the style guidelines and recommended practices of this project
  • I have performed a self-review of my own code and fixed the problems I found
  • I have tested my change that my fix is effective or that my feature works, and that it does not break existing functionality.

Any TODOs?

A summary of any remaining work required to complete the requirements of the task.

Summary by CodeRabbit

  • 新功能
    • 优化了最新通知的处理方式,提高了组件的性能。
  • 文档
    • 更新了导出实体的命名,以反映最新的实现。

Copy link

vercel bot commented Sep 16, 2024

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

Name Status Preview Comments Updated (UTC)
notification ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 16, 2024 7:13am

Copy link

coderabbitai bot commented Sep 16, 2024

Walkthrough

此次更改涉及将 NoticeList.tsx 文件中的状态变量替换为引用(ref)来管理最新通知元素。原有的 latestNotice 状态变量被 latestNoticeRef 替换,后者是通过 useRef 创建的引用。这一修改改变了组件内对最新通知的访问和更新方式,useEffect 钩子现在直接将最新通知元素赋值给 latestNoticeRef.current,从而避免了不必要的重新渲染。

Changes

文件 更改摘要
src/NoticeList.tsx latestNotice 状态变量重命名为 latestNoticeRef,并改用 useRef 进行管理。

Poem

在兔子窝里欢声笑,
最新通知来报道。
用引用代替状态,
组件运行更轻巧。
兔子跳跃乐无边,
效率提升真欢喜! 🐇✨


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f972527 and 2b142e0.

Files selected for processing (1)
  • src/NoticeList.tsx (4 hunks)
Additional comments not posted (3)
src/NoticeList.tsx (3)

49-49: 很好的优化!

latestNotice 从状态变量改为 ref 是一个很好的优化。它避免了由状态更新引起的不必要的重新渲染。这种改变没有明显的问题。


75-75: 修改正确

useEffect 钩子中的 latestNotice 替换为 latestNoticeRef.current 与之前使用 ref 而不是状态的改变是一致的。直接将最新的通知元素分配给 latestNoticeRef.current 是正确的。这种改变没有明显的问题。


129-130: 修改正确

scaleX 的计算中将 latestNotice 替换为 latestNoticeRef.current,与之前使用 ref 而不是状态的改变是一致的。使用 latestNoticeRef.current 访问最新通知元素的 offsetWidth 是正确的。这种改变没有明显的问题。


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 generate interesting stats about this repository and render them as a table.
    -- @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 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 Sep 16, 2024

Codecov Report

Attention: Patch coverage is 80.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 99.73%. Comparing base (f972527) to head (2b142e0).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/NoticeList.tsx 80.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master     #361   +/-   ##
=======================================
  Coverage   99.73%   99.73%           
=======================================
  Files           7        7           
  Lines         759      759           
  Branches      124      124           
=======================================
  Hits          757      757           
  Misses          2        2           

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

@afc163 afc163 merged commit 6811a31 into react-component:master Sep 16, 2024
10 of 11 checks passed
@yoyo837
Copy link
Member

yoyo837 commented Sep 17, 2024

130行未被用例覆盖

@@ -46,7 +46,7 @@ const NoticeList: FC<NoticeListProps> = (props) => {
const { classNames: ctxCls } = useContext(NotificationContext);

const dictRef = useRef<Record<string, HTMLDivElement>>({});
const [latestNotice, setLatestNotice] = useState<HTMLDivElement>(null);
const latestNoticeRef = useRef<HTMLDivElement>(null);
Copy link
Member

Choose a reason for hiding this comment

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

state 换成 ref 在 concurrent 下容易出问题,应该尽量以 effect 里配对做 state 更新。

Copy link
Member

Choose a reason for hiding this comment

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

另外缺 test case

Copy link
Member

Choose a reason for hiding this comment

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

ref 不会触发更新,会导致样式计算问题

Copy link
Contributor Author

Choose a reason for hiding this comment

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

我在motion修复了内存泄漏之后 再去测const [latestNotice, setLatestNotice] = useState(null); 好像没有泄露的问题 可能被motion那块影响到了我的判断 麻烦各位大佬测试下 给各位大佬添麻烦了😊 @afc163 @MadCcc
我回去会再去测试这块

Copy link
Member

Choose a reason for hiding this comment

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

那先补下上面说的那些?

MadCcc added a commit that referenced this pull request Sep 27, 2024
MadCcc added a commit that referenced this pull request Sep 27, 2024
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.

5 participants