react怎么写点击事件-创新互联
小编给大家分享一下react怎么写点击事件,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

react写点击事件的方法:1、使用bind绑定,代码为【this.clicked.bind(this,"hello world")】;2、使用箭头函数,代码为【onClick={(event)=>this.clicked("hello】。
react写点击事件的方法:
1、bind绑定
第一个参数指向this,第二个参数开始才是事件函数接收到的参数,事件对象event默认是最后一个参数。
...
clicked(param,event){
console.log(param) //hello world
console.log(event.target.value) //按钮
}
render(){
return (
)
}
...这里的话绑定this可以统一写,这样代码看起来整洁点。
...
constructor(props){
super(props);
this.state = {};
this.checkMenu = this.checkMenu.bind(this);
}
clicked = (param)=>{
return (event)=>{
console.log(event.target.value); // 按钮
console.log(param); // hello
}
}
render(){
return (
)
}
...2、箭头函数
箭头函数若要传事件对象event的话,需要在箭头函数中把event作为参数传递给触发的事件。
...
clicked(param,event){
console.log(param) //hello world
console.log(event.target.value) //按钮
}
render(){
return (
)
}
...看完了这篇文章,相信你对react怎么写点击事件有了一定的了解,想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
文章名称:react怎么写点击事件-创新互联
转载源于:http://www.jxjierui.cn/article/dgepjh.html


咨询
建站咨询
