在一個button的onpress方法中傳進一個函數back,這個函數中如果用到this的話,需要將back中的this進行綁定操作。
1、傳入時進行綁定
onPress={this.back.bind(this)}
2、在constructor中綁定
constructor(props){
super(props)
this.state = {
show : true,
}
this.back = this.back.bind(this);
}
3、在函數定義時進行綁定
onPress={this.back}
....
back = () => {
const {navigator} = this.props;
navigator.pop();
}
()=>{} 這種形式的代碼,語法規定就是(function(){}).bind(this),即自動添加了bind this。 參考鏈接