react事件传参方式

第一种:绑定this的情况

<button
  style={{ color: this.state.themeColor }}
  onClick={this.handleSwitchColor.bind(this, ...)}
>Blue</button>

第二种:ES6 箭头函数情况

<button
  style={{ color: this.state.themeColor }}
  onClick={() => {
    this.handleSwitchColor(...)
}}
>Blue</button>

接收参数方式(需要通过 bind 方法来绑定参数,第一个参数指向 this,第二个参数开始才是事件函数接收到的参数):

<button onClick={this.handleSwitchColor.bind(this, props0, props1, ...}></button>
 
handleSwitchColor(porps0, props1, ..., event) {
    // your code here
}

 推荐使用ES6写法,箭头函数除了方便之外还有而一个特征就是将函数的this绑定到其定义时所在的上下文。这个特征也可以帮助我们解决这个问题。个人认为,使用箭头函数使得 JavaScript 更加接近面向对象的编程风格。

<div onClick={() => this.handleClick()}>
  You {text} this. Click to toggle.
</div>

this 的总结

this 的本质就是:this跟作用域无关的,只跟执行上下文有关。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hidetou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值