javascript - 看不到附加的事件功能,但 noop
问题描述
问题是,虽然我handleClear
在下面的类中附加了事件(例如),但是,当我检查onClick
按钮 dom 中的事件时,它显示的是一个空方法noop() {}
。结果,它无法按预期清除。
奇怪的是,有时如果我设置断点,处理程序方法就会在那里。我怀疑事件附件与时间有关。但我无法确定附加事件功能的时间和地点。任何人都可以提供一些解决这个问题的提示吗?或者提供一些见解?
class ClearHistory extends PureComponent {
handleCancel = () => {
console.log('handleCancel');
}
handleClear = (event) => {
console.log('handleClear');
}
render() {
return (
<div>
<Button key='cancel' type='button' onClick={this.handleCancel} >
Cancel
</Button>
<Button key='remove' type='button' onClick={this.handleClear} >
Clear
</Button>
</div>
);
}
}
解决方案
我创建了沙箱来测试您的组件,并重命名为 html 标记,因为该组件没有任何 ui-library。
https://codesandbox.io/s/clearhistory-fq69b?file=/src/ClearHistory.js
它有效,但是:
这很好,因为反应本身负责事件绑定。
推荐阅读
- bash - 无法从 pg_isready 的 bash shell 输出中获取变量的值
- python - SlidingWindows Python Apache Beam 复制数据
- gulp - 为什么 gulp-babel 不创建任何文件?
- python - 将字符列编码为序数,但保持数字列相同
- sqlite - 如何将内存中的sqlite数据库保存到perl中的文件中?
- javascript - 为什么 post 方法不会将新对象写入文件?
- visual-studio-2012 - 在 VS Data tools 2012 中调试 SSIS 包时,本地窗口显示空变量
- python - 如何使用python在图像中查找有界区域的RGB值?
- python - 为什么我在尝试使用 Evernote 进行身份验证时收到 KeyError?
- android - 尝试通过(OnItemClickListener)在片段之间添加选定的图像视图,在应用程序崩溃的第二个 ImageView 上