Skip to content

fix(components-library-react): clear removed event handler cache#19301

Open
Cookie726 wants to merge 1 commit into
NervJS:mainfrom
Cookie726:fix/clear-removed-event-handler-cache
Open

fix(components-library-react): clear removed event handler cache#19301
Cookie726 wants to merge 1 commit into
NervJS:mainfrom
Cookie726:fix/clear-removed-event-handler-cache

Conversation

@Cookie726

@Cookie726 Cookie726 commented Jun 4, 2026

Copy link
Copy Markdown

这个 PR 做了什么? (简要描述所做更改)

修复了 Taro H5 下 React 组件库事件解绑后无法用同一 handler 重新绑定的问题。

问题可通过如下代码复现:

import { View } from '@tarojs/components'
import { useCallback, useState } from 'react'

export default function Page() {
  const [loading, setLoading] = useState(false)

  const handleClick = useCallback(async () => {
    console.log('click')
    setLoading(true)
    await new Promise(resolve => setTimeout(resolve, 1000))
    setLoading(false)
  }, [])

  return (
    <View onClick={loading ? undefined : handleClick}>
      Button
    </View>
  )
}

在上述场景中,onClick 会经历 handleClick -> undefined -> handleClick 的切换:

  • 第一次点击可以正常触发
  • loading 恢复为 false 后,第二次点击在 Taro H5 下失效
  • 同样的代码在纯 React 和小程序端可以正常工作

原因是 syncEvent 在移除事件监听时,没有同步清理 node.__events 中的缓存,导致后续再次传入同一个 handler 时被误判为事件已存在,进而跳过重新绑定。

本 PR 的修复内容:

  • 在移除事件监听时同步删除 eventStore[eventName]
  • 保证 handler -> undefined -> same handler 的场景下可以正确重新绑定事件
  • 新增对应回归测试,覆盖该问题路径

这个 PR 是什么类型? (至少选择一个)

  • 错误修复 (Bugfix) issue: fix #
  • 新功能 (Feature)
  • 代码重构 (Refactor)
  • TypeScript 类型定义修改 (Types)
  • 文档修改 (Docs)
  • 代码风格更新 (Code style update)
  • 构建优化 (Chore)
  • 其他,请描述 (Other, please describe):

这个 PR 涉及以下平台:

  • 所有平台
  • Web 端(H5)
  • 移动端(React-Native)
  • 鸿蒙(Harmony)
  • 鸿蒙容器(Harmony Hybrid)
  • ASCF 元服务
  • 快应用(QuickApp)
  • 所有小程序
  • 微信小程序
  • 企业微信小程序
  • 京东小程序
  • 百度小程序
  • 支付宝小程序
  • 支付宝 IOT 小程序
  • 钉钉小程序
  • QQ 小程序
  • 飞书小程序
  • 快手小程序
  • 头条小程序

Summary by CodeRabbit

发布说明

  • Bug Fixes
    • 改进事件监听移除逻辑,确保已移除的事件不会在内部缓存中留下陈旧引用,增强事件系统的稳定性和可靠性。

@coderabbitai

coderabbitai Bot commented Jun 4, 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

Run ID: 0e38fc29-b7a0-4195-8ed7-ca501e225b92

📥 Commits

Reviewing files that changed from the base of the PR and between 0db37ec and ee62bd3.

📒 Files selected for processing (1)
  • packages/taro-components-library-react/src/react-component-lib/utils/attachProps.ts

总览

attachProps.ts 中 syncEvent 函数的事件监听移除逻辑得到完善。当需要移除事件监听时,不仅移除旧的处理器,还同步从内部事件缓存 eventStore 中删除对应条目,确保缓存一致性。

变更

事件监听移除与缓存清理

层级 / 文件 摘要
事件移除时的缓存清理
packages/taro-components-library-react/src/react-component-lib/utils/attachProps.ts
syncEvent 函数中完善 !newEventHandler 分支的逻辑,增加条件判断和 delete eventStore[eventName] 操作,确保事件监听移除后内部缓存同步清理。

代码审查工作量

🎯 2 (简单) | ⏱️ ~8 分钟


🐰 事件缓存整理清,
监听移除不留痕。
一笔一划细致行,
缓存清爽心欢欣!

🚥 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 PR 标题清晰准确地总结了主要变更:修复事件处理器缓存清理问题,与代码变更内容完全相符。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 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 and usage tips.

This was referenced Jun 6, 2026
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.

1 participant