用的react-native 页面跳转都用的react navigation的navigate方法
流程是在登录页按enter键,登录,跳转到主页home,然后主页点击进入页面A,按enter键可以执行这个页面的方法a
问题是,在登录页首页按enter键,都会调用登录方法,在页面A按enter会调用登录方法和方法a,离开页面A只调用登录方法
从登录页跳转到首页时,componentWillUnmount没触发,但是从页面A跳转到home会触发(navigate和goBack都会触发)
登录页相关代码
componentDidMount(){
var _this = this
this.handler = DeviceEventEmitter.addListener("keyup",(e)=>{
console.log(_this.props.route.name)
if(e.which == 66 && _this.state.username && _this.state.password){
_this.submit()
}
if(e.which == 131){ //f1
_this.changeFocus()
}
})
}
componentWillUnmount(){
console.log("离开Login")
this.handler.remove()
}
页面A相关代码
componentDidMount(){
var _this = this
this.handler = DeviceEventEmitter.addListener("keyup",function(e){
if(e.which == 66){
_this.bundle();
return;
}
if(e.which == 131){ //f1
_this.changeFocus(_this.state.flags[(++_this.index)%5])
}
})
}
componentWillUnmount(){
console.log("离开")
this.handler.remove()
}