How to handle exit app in one screen not in the whole app - React Native?

后端 未结 2 731
情歌与酒
情歌与酒 2021-01-05 18:23

I have some issue with BackHandler, the issue is

when running the app and go to the let\'s say Sign Up screen and touch the back in my mobile they w

2条回答
  •  甜味超标
    2021-01-05 18:44

    You have to get the navigation object of your ROOT navigator by calling dangerouslyGetParent recursively

    For Example If your have that heirarchy:

    • BottomTabs (TabNavigator)

      • Browser (StackNavigator)
      • Settings (StackNavigator)
        • About (Screen)

    And you want to get the current routeName in your androidBackHandler which is wired in your Browser screen, then you should get the navigation object of your BottomTabs by calling dangerouslyGetParent twice (which is the depth of your navigation tree):

    const bottomTabsNavigation = navigation
          .dangerouslyGetParent()
          .dangerouslyGetParent();
    

    If you tried to console.log your bottomTabsNavigation when you're on About screen, you should see something like that

提交回复
热议问题