美文网首页
react中的this

react中的this

作者: yzr_0802 | 来源:发表于2017-08-24 15:29 被阅读0次

在ES6写法中的react:React.Component创建组件,其成员不会自动绑定this,需要手动绑定。
手动绑定的方法有3种:
onchange(){
console.log("ok!")
}
1)构造函数中完成绑定
constructor(){
super()
this.onchange=this.onchange.band(this);
}
2)可以在调用的时候使用method.bind(this)完成绑定
<div onClick={this.onchange.bind(this)}></div>
3)可以使用箭头函数arraw function绑定
<div onClick={()=>{this.onchange();}}></div>

相关文章

网友评论

      本文标题:react中的this

      本文链接:https://www.haomeiwen.com/subject/krredxtx.html