How to reset TabNavigator when user logs out (from other screen)

后端 未结 3 1161
心在旅途
心在旅途 2020-11-27 19:49

Here is my project file hierarchy

RootTabNavigator
    | AuthStackNavigator // I want to go back to this navigator
          | AuthoScreen
    | Welcome Scre         


        
3条回答
  •  清歌不尽
    2020-11-27 20:24

    You can define custom navigation logic by extending the router. To accomplish what you want that you described in the project file hierarchy in your question you can do something like this below.

    MainTabNavigator.js

    ...
    
    RootTabNavigator.router.getStateForAction = (action, state) => {
      if (state && action.type === 'GoToAuthScreen') {
        return {
          ...state,
          index: 0,
        };
      }
    
      return RootTabNavigator.router.getStateForAction(action, state);
    };
    
    MainTabNavigator.router.getStateForAction = (action, state) => {
      if (state && action.type === 'GoToAuthScreen') {
        return {
          ...state,
          index: 0,
        };
      }
    
      return MainTabNavigator.router.getStateForAction(action, state);
    };
    
    MenuStackNavigator.router.getStateForAction = (action, state) => {
      if (state && action.type === 'GoToAuthScreen') {
        return {
          ...state,
          index: 0,
        };
      }
    
      return MenuStackNavigator.router.getStateForAction(action, state);
    };
    

    In the Menuo Screen file

    componentWillReceiveProps(nextProps) {
      if ( nextProps.token == undefined || _.isNil(nextProps.token) ) {
        const goToAuthScreen = () => ({
          type: 'GoToAuthScreen',
        });
    
        nextProps.navigation.dispatch(goToAuthScreen);
        ...
      }
    }
    

提交回复
热议问题