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

refactor: make ref stable #62

Merged
merged 2 commits into from
Feb 11, 2025
Merged

refactor: make ref stable #62

merged 2 commits into from
Feb 11, 2025

Conversation

zombieJ
Copy link
Member

@zombieJ zombieJ commented Feb 10, 2025

ref 返回 obj 时,每次 render 都会做一次 fill -> null -> fill 的填充,导致和原本的渲染逻辑不一致。这里让 refObj 变成稳定态以与 rc 版本保持一致。

Summary by CodeRabbit

  • 重构
    • 优化了动画组件的内部状态管理和引用处理,使得动画效果在展示时更加稳定、响应更及时,从而提升整体视觉体验。

Copy link

vercel bot commented Feb 10, 2025

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

Name Status Preview Comments Updated (UTC)
motion ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 11, 2025 2:55am

Copy link

codecov bot commented Feb 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.47%. Comparing base (538b35f) to head (39399ec).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #62      +/-   ##
==========================================
+ Coverage   98.45%   98.47%   +0.02%     
==========================================
  Files          11       11              
  Lines         388      394       +6     
  Branches      106      108       +2     
==========================================
+ Hits          382      388       +6     
  Misses          6        6              

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

Copy link

coderabbitai bot commented Feb 10, 2025

Walkthrough

此次变更主要对 CSSMotion 组件和 useStatus Hook 进行了重构。在 CSSMotion 组件中,不再直接解构 status,而是通过调用 getStatus 函数来获取当前状态。同时,组件中用于暴露引用的方式也做了调整,使用 Object.defineProperties 定义 refObj 对象,确保 inMotion 总是通过动态调用 getStatus 获取最新状态。useStatus Hook 则从直接返回状态修改为返回获取状态的函数,内部逻辑保持不变。

Changes

文件路径 修改说明
src/CSSMotion.tsx 重构状态获取方式,从直接解构 status 改为使用 getStatus 函数;调整 ref 暴露方式,使用 Object.defineProperties 定义 refObj 对象。
src/hooks/useStatus.ts 修改返回类型,将直接返回 MotionStatus 改为返回一个获取状态的函数,调整相关返回值。

Sequence Diagram(s)

sequenceDiagram
    participant P as Parent Component
    participant C as CSSMotion Component
    participant U as useStatus Hook

    P->>C: 渲染组件并传入 ref
    C->>U: 调用 useStatus 获取状态信息
    U-->>C: 返回 [getStatus, ...]
    C->>C: 使用 getStatus 创建 refObj (包含 dynamic inMotion)
    P->>C: 通过 ref 调用 inMotion() 获取当前状态
    C->>U: 执行 getStatus() 获取最新状态
    U-->>C: 返回当前状态
Loading

Possibly related PRs

Poem

我是一只快乐的小兔子,
代码花园中跳跃不停,
CSSMotion换了新装扮,
状态管理如胡萝卜般清新,
用函数呼唤最新动态,
Ref对象聪明又灵动,
祝我们代码路上春风得意! 🐰💕


📜 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 bb9dda7 and 39399ec.

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

150-156: 状态获取方式的改进很棒!

使用 getStatus 函数来获取状态值是一个很好的改进,这样可以确保我们始终能获取到最新的状态值。


166-185: 优秀的 ref 稳定性实现!

通过 useMemoObject.defineProperties 的组合使用,很好地解决了 ref 稳定性的问题:

  • 使用 useMemo 确保 refObj 的稳定性
  • 通过 Object.defineProperties 实现了属性的懒加载
  • useImperativeHandle 使用空依赖数组确保引用稳定

这个实现完美地采纳了之前关于使用 useMemo 的建议。


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.

nativeElement: getDomElement(),
inMotion: () => status !== STATUS_NONE,
}));
const [refObj] = React.useState<CSSMotionRef>(() => {
Copy link
Member

Choose a reason for hiding this comment

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

为啥不用 useMemo 大佬?

Copy link
Member Author

Choose a reason for hiding this comment

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

可以的,改着改着就不是原来的样子了。我改成 useMemo

@zombieJ zombieJ merged commit 4dd7ad1 into master Feb 11, 2025
10 checks passed
@zombieJ zombieJ deleted the stableRef branch February 11, 2025 06:22
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