Skip to content

fix(useDelayState): cancel pending updates on unmount - #806

Open
nrps9909 wants to merge 1 commit into
react-component:masterfrom
nrps9909:codex/cancel-delay-state-on-unmount
Open

fix(useDelayState): cancel pending updates on unmount#806
nrps9909 wants to merge 1 commit into
react-component:masterfrom
nrps9909:codex/cancel-delay-state-on-unmount

Conversation

@nrps9909

@nrps9909 nrps9909 commented Aug 26, 2026

Copy link
Copy Markdown

Summary

  • cancel a pending animation-frame or timeout update when useDelayState unmounts
  • reuse the hook's existing cancelPending path for cleanup
  • cover both supported delay modes with regression tests

Problem

useDelayState cancels pending work before scheduling a replacement, but it did not cancel that work on unmount. In React 19/Vitest environments this can leave the callback alive beyond jsdom teardown and surface errors such as window is not defined.

Fixes #805.

Verification

  • Exact-base regression: both new cases failed because one timer remained after unmount
  • Focused test: 10/10 passed
  • Full suite: 29/29 suites passed; 184 passed, 1 skipped
  • TypeScript passed
  • Focused ESLint passed
  • Prettier passed
  • ESM and CJS compilation completed with 0 errors (6 existing unrelated unused-disable warnings)
  • git diff --check passed

I audited current open PRs for duplicates and target-file overlap. #798 also touches src/hooks/useDelayState.ts, but only changes type formatting/inference around useEvent; it does not address unmount cleanup or touch this regression test.

AI assistance disclosure: Codex was used to trace the lifecycle, write the focused regression tests, audit open PR overlap, and run verification. The behavior and results above were verified against the exact current base and this PR head.

Summary by CodeRabbit

  • Bug 修复

    • 修复组件卸载时仍执行延迟状态更新的问题。
    • 组件卸载后会自动取消待处理的动画帧或定时器,避免意外更新。
  • 测试

    • 增加针对帧数和毫秒延迟场景的卸载清理测试。

@vercel

vercel Bot commented Aug 26, 2026

Copy link
Copy Markdown

@nrps9909 is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Aug 26, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 25da6cbb-dd06-4287-be3e-53340ba0d428

📥 Commits

Reviewing files that changed from the base of the PR and between d1045f4 and cb17e58.

📒 Files selected for processing (2)
  • src/hooks/useDelayState.ts
  • tests/useDelayState.test.tsx

Included review availability: Your plan provides up to 4 included reviews per hour; 3 remain after this review.


Walkthrough

useDelayState 现在会在组件卸载时调用 cancelPending,取消待处理的 requestAnimationFrame 或定时器更新。新增参数化测试覆盖两种延迟方式,并验证卸载后定时器已清除。

Changes

延迟状态清理

Layer / File(s) Summary
卸载清理与参数化验证
src/hooks/useDelayState.ts, tests/useDelayState.test.tsx
组件卸载时调用 cancelPending。测试覆盖帧延迟和毫秒延迟,并确认卸载后所有定时器均已清除。

Estimated code review effort: 2 (简单) | ~10 minutes

Merge Risk: ⚪ Minimal · up to cb17e

This localized change cancels pending delayed updates when the hook unmounts, with regression tests covering both delay modes. No actionable merge-blocking risk remains after normal checks and review.

Poem

小兔挥爪,帧与定时器停止奔跑
卸载发生,待处理更新安静退场
两种延迟,都经过测试检查
定时器清空,窗口不再惊慌
代码轻轻落地,耳朵闪闪发光

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确说明了在 useDelayState 卸载时取消待处理更新这一主要变更,内容简洁且明确。
Linked Issues check ✅ Passed PR 实现了 Issue #805 的核心要求:组件卸载时调用现有的 cancelPending,取消 timeout 和 animation-frame 模式下的待处理更新,并增加对应回归测试。
Out of Scope Changes check ✅ Passed 代码变更和测试变更均直接服务于 Issue #805 及 PR 目标,未发现无关或超出范围的修改。
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

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.

useDelayState throws error in vitest

1 participant