簡體   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