react中传递事件对象的方法:1、使用“{(e) => this.deleteRow(id, e)}”方式传递;2、通过“{this.deleteRow.bind(this, id)}”方式传递。

本教程操作环境:windows7系统、react17.0.1版本,Dell G3电脑。
推荐:《javascript基础教程》
向事件处理程序传递参数(事件对象)
给函数传递额外参数:以下两种方式
<button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button>
<button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>上述两种方式是等价的,分别通过 箭头函数 和 Function.prototype.bind 来实现。
上面两个例子中,参数 e 作为 React 事件对象将会被作为第二个参数进行传递。通过箭头函数的方式,事件对象必须显式的进行传递,但是通过 bind 的方式,事件对象以及更多的参数将会被隐式的进行传递。
值得注意的是,通过 bind 方式向监听函数传参,在类组件中定义的监听函数,事件对象 e 要排在所传递参数的后面,例如:
class Popper extends React.Component{
constructor(){
super();
this.state = {name:'Hello world!'};
}
preventPop(name, e){ //事件对象e要放在最后
e.preventDefault();
alert(name);
}
render(){
return (
<div>
<p>hello</p>
{/* Pass params via bind() method. */}
<a href="https://reactjs.org" onClick={
this.preventPop.bind(this,this.state.name)
}>Click</a>
</div>
);
}
}更多编程相关知识,请访问:编程学习!!
以上就是react中怎么传递事件对象的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号