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

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);
}

原文地址:https://www.cnblogs.com/zhangxintong1314/p/6565599.html