javascript - 反应功能自定义按钮未注册点击
问题描述
所以这是我的功能按钮组件
export const MyButton = (props) => {
return (
<button className="myButton">{props.text}</button>
)
}
我在另一个页面中使用了它
<MyButton text="Update" onClick={this.checkIt} />
该 checkIt 函数只是控制台记录我使用它的类的状态。但由于某种原因,它根本没有注册点击。我该如何解决呢?我以类似的方式实现了我使用的输入字段props.onChange(event.target.value)
并且工作正常。
解决方案
您应该在子组件中附加事件,否则它根本不会绑定到实际 UI!
解决方案1:
export const MyButton = ({text, ...others}) => {
return (
<button className="myButton" {...others}>{text}</button>
)
}
解决方案2:
export const MyButton = ({text, onClick}) => {
return (
<button className="myButton" onClick={onClick}>{text}</button>
)
}
推荐阅读
- arrays - 如何使用计时器 VB.NET 每 3 秒更改一次标签文本
- visual-studio - 根据页面大小自动调整 Visual Studio Designer
- flask - Flask 找不到应用程序文件
- facebook - 页面公共内容访问速率限制
- javascript - 用于 .split() 的正则表达式以分隔除引号之外的空格上的字符串
- python - Tkinter 在 python 3.6 中出现以下错误:TclError: NULL main Window
- php - PayPal 付款执行错误:此操作不支持方法类型
- linux - 我的 bash 脚本在 kill 命令后不会执行命令
- r - 通过使用 navbarPanel() 并隐藏导航栏来构建多页闪亮的应用程序用户端(在 ui.R 中)?
- protractor - 如何使用量角器选择下拉菜单?