import React, { Component } from 'react';
export default class App extends Component {
state = {
count:1
}
addM(m,event) {
console.log(event);
this.setState({count:this.state.count + m})
}
add (params1,params2,event) { // event是最后一个参数
this.setState({count:this.state.count + params1 + params2})
}
render() {
return (
<>
<div>count:{ this.state.count}</div>
{/* 1.react事件传递参数1:使用箭头函数包裹调用参数的函数 */}
<button onClick={(event) => {
const m = 100
this.addM(m,event)
}}>加m</button>
{/* 2.react事件传递参数2 */}
<button onClick={this.add.bind(this,1000,100)}>加</button>
</>
);
}
}
React两种传递参数的方式
最新推荐文章于 2024-02-02 19:01:46 发布
这篇博客探讨了在React中如何通过事件处理函数传递参数,包括使用箭头函数和bind方法。示例代码展示了在按钮点击事件中更新状态并传递不同参数的方法。
3024

被折叠的 条评论
为什么被折叠?



