React Native navigating between Nested StackNavigator

烂漫一生 提交于 2019-12-08 17:30:24

问题


I have the three navigators below, I am navigating to the DashBoard Screen after the login is completed but I have an issue when I want to logout from the DashBoard Screen, this.props.navigation.navigate('Login') is working fine but I want to clear the stack when signout button is pressed.

const DashBoardStackNavigator = createStackNavigator({
      DashBoard: DashBoard,
      Second:Second,
      Third:Third
    })


const BottomTabNavigator = createBottomTabNavigator({
  DashBoardStackNavigator,
  Account,
  Report,
  Members
}})

const AppStackNavigator = createStackNavigator({
  Login: Login,
  BottomTabNavigator: BottomTabNavigator

})


export default createAppContainer(AppStackNavigator)

I tried the following with no luck

const resetAction = StackActions.reset({
  index: 0,
  actions: [NavigationActions.navigate({ routeName: 'Login' })],
});
this.props.navigation.dispatch(resetAction);

Error: there is no route defined for key Login, Must Be one of DashBoard


回答1:


Try this

 const navigateAction = StackActions.reset({
            index: 0,
            key: null,
            actions: [NavigationActions.navigate({ routeName: 'Login' })]
        })
        this.props.navigation.dispatch(navigateAction)



回答2:


I suggest you to use a SwitchNavigator, as suggested by the following official documentation: https://reactnavigation.org/docs/en/auth-flow.html

In your case, just replace the last StackNavigator with a SwitchNavigator:

const DashBoardStackNavigator = createStackNavigator({
      DashBoard: DashBoard,
      Second:Second,
      Third:Third
    })


const BottomTabNavigator = createBottomTabNavigator({
  DashBoardStackNavigator,
  Account,
  Report,
  Members
}})

const AppStackNavigator = createSwitchNavigator({
  Login: Login,
  BottomTabNavigator: BottomTabNavigator

})


export default createAppContainer(AppStackNavigator) 

Then just navigate to the Login:

this.props.navigation.navigate('Login')

It will automatically reset the stack when you switch between the bottomTabNavigator stack and the login stack, blocking all back actions between the two.



来源:https://stackoverflow.com/questions/54704752/react-native-navigating-between-nested-stacknavigator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!