繁体   English   中英

当在 react-native 中按下硬件后退按钮时,无法对同一组件执行 2 个操作,以下是有关反处理程序的代码

[英]Not able to perform 2 actions on the same component when hardware back button is pressed in react-native,below are the code regarding the backhandler

 backAction = () => {
    if (this.props.navigation.isFocused() && !this.state.otpScreen) {
     
  this.setState({ showLoginScreen: true });
  return true;

} else if(this.state.showLoginScreen) {
  this.props.navigation.dispatch(CommonActions.reset({
    index: 0,
    routes: [
      { name: 'Login' },
    ],
  }))
  
  return false;
}

};

上面的代码用于应该由硬件后按完成的操作,首先它应该显示一个屏幕,第二次按下它应该退出应用程序,otp 屏幕和登录屏幕都在同一个组件上,我是只是根据条件隐藏......第一次它按照条件工作,但第二次不是。

 componentDidMount() {
this.focusListener = this.props.navigation.addListener('focus', () => {
  this.setState({
    mobile: '',
    showLoginScreen: true,
  });
});
this.getDataFromStorage();
AsyncStorage.setItem('countryCode', '+91');
BackHandler.addEventListener(
  "hardwareBackPress",
  this.backAction
);


}
  componentWillUnmount() {
    clearInterval(this.interval)
    BackHandler.addEventListener('hardwareBackPress', () => { return false })
  }

任何人都可以帮助我如何做到这一点,在此先感谢

我猜您的问题是您无法访问侦听器内状态的“当前”值。

更多细节在这里

尝试使用引用而不是状态

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM