首页 > 其他 > 详细

react 函数bind(this)的三种方式

时间:2017-03-17 14:40:03      阅读:383      评论:0      收藏:0      [点我收藏+]

1.在调用地方直接bind(this)

handleClick(){
this.setState({
word2:‘word2 changed‘
})
}

<button onClick={this.handleClick.bind(this)}>点击</button>

2、使用ES6 箭头函数

handleClick=()=>{
this.setState({
word2:‘word2 changed‘
})
}

<button onClick={this.handleClick}>点击</button>

3、构造函数中bind(this)

constructor(props){
super(props);
this.state=({
word2:‘word2‘
})
this.handleClick = this.handleClick.bind(this);
}

 

react 函数bind(this)的三种方式

原文:http://www.cnblogs.com/zhangxintong1314/p/6565599.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!