用的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()
}
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。
因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。
A 和 B 的本机堆栈导航器。导航到 A 后,将componentDidMount调用它。当压入 B 时,它componentDidMount也被调用,但 A 仍然安装在堆栈上,componentWillUnmount因此不会被调用。
当从 B 回到 A 时,componentWillUnmountB 被称为,但componentDidMountA 不是因为 A 一直在挂载
React Navigation 向订阅它们的屏幕组件发出事件。我们可以通过监听事件来focus分别blur了解屏幕何时聚焦或失焦。
function Profile({ navigation }) {
React.useEffect(() => {
const unsubscribe = navigation.addListener('focus', () => {
// Screen was focused
// Do something
});
return unsubscribe;
}, [navigation]);
return <ProfileContent />;
}