Not been able to navigate to login screen , after logout.

Viewed 782

I am using ReactNavigation and after SignIn , i enter my home page. My home page has 4 tabs, and on 4th tab i have logout button. After clicking on logout button , i should get back to SIGN IN screen.

The issue i am facing is , i am not been able to reset the TAB NAVIGATOR.

          this.props.navigation.dispatch({
        type: NavigationActions.NAVIGATE,
        routeName: 'SignIn',
        action: {
          type: NavigationActions.RESET,
          index: 0,
          actions: [{type: NavigationActions.NAVIGATE, routeName: 'SignIn'}]
        }
      })

I tried applying above chunk of code to navigate , but it did not work. Please tell me , how to get out of tab navigator.

2 Answers

Your root-level navigation should be a StackNavigator with children looking something like this:

  • Welcome screen
  • Login screen
  • Tab Navigator (for logged in home screen)
    • Tab 1
    • Tab 2
    • Tab 3
    • Tab 4 (with logout button)

To reset the TabNavigator, you shouldn't dispatch an action of type NavigationActions.NAVIGATE because that will push onto the current navigation stack, instead of removing the tab navigator from the state. Instead, consider using a Reset action. So the code within your logout function should have:

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

onPress of Logout we are calling below code

  const resetAction = NavigationActions.reset({
  index: 0,
  actions: [NavigationActions.navigate({ routeName: "SignIn" })],
    key: null 
  });
  this.props.navigation.dispatch(resetAction);

This is our AppNavigation file is there any way to reset app on Logout

const PrimaryNav = StackNavigator(
  {
    SignIn: {
      screen: SignIn,
      navigationOptions: {
        headerMode: "none",
        header: null
      }
    },
    SignUp: {
      screen: SignUp,
      navigationOptions: {
        headerMode: "none",
        header: null
      }
    },
    TabsScreen: {
      screen: TabsScreen,
      navigationOptions: {
        headerMode: "float",
        headerTitleStyle: { color: "#ffffff" },
        gesturesEnabled: true,
        header: null,
      }
    },
)
export const sideMenuStack = StackNavigator({
    SideMenu: {
        screen: SideMenu,
        navigationOptions: {
            headerMode: "none",
            header: null
        }
    },
    Settings: {
        screen: Settings,
        navigationOptions: ({ navigation }) => ({
            title: "Settings",
        })
    },
    SignIn: {
      screen: SignIn,
      navigationOptions: {
        headerMode: "none",
        header: null
      }
    },
)
export const FeedsStack = StackNavigator({
    FeedsStack: {
        screen: FeedsStack,
        navigationOptions: {
            headerMode: "none",
            header: null
        }
    },
    Post: {
        screen: Post,
        navigationOptions: ({ navigation }) => ({
            title: "Post",
        })
    },
)
export const UserStack = StackNavigator({
    UserStack: {
        screen: UserStack,
        navigationOptions: {
            headerMode: "none",
            header: null
        }
    },
    Friends: {
        screen: Friends,
        navigationOptions: ({ navigation }) => ({
            title: "Friends",
        })
    },
)
export const TabView = TabNavigator(
  {
    FeedsStack: {
      screen: FeedsStack,
      navigationOptions: {
        tabBarLabel: "Feeds",
        scrollEnabled: true,
        focussed: true,
        tabBarIcon: ({ tintColor }) =>
          <Image
            source={Images.feedsActive}
            style={[styles.icon, { tintColor: tintColor }]}
          />
      }
    },
    UserStack: {
      screen: UserStack,
      navigationOptions: {
        tabBarLabel: "Users",
        tabBarIcon: ({ tintColor }) =>
          <Image
            source={Images.UserActive}
            style={[styles.icon, { tintColor: tintColor }]}
          />
      }
    },
    sideMenuStack: {
      screen: sideMenuStack,
      navigationOptions: {
        tabBarLabel: "More",
        headerMode: "screen",
        tabBarIcon: ({ tintColor }) =>
          <Image
            source={Images.moreActive}
            style={[styles.icon, { tintColor: tintColor }]}
          />
      }
    }
  },
  //Default configurations for Tabs
  {
    tabBarPosition: "bottom",
    animationEnabled: true,
    tabBarOptions: {
       indicatorStyle:{
        borderBottomColor:'#32a1fe',
        borderBottomWidth:3
      },
      showLabel: true,
      backBehavior: "none",
      showIcon: true,
      activeTintColor:"#338AD8",
      inactiveTintColor:"#BEC5CB",
      upperCaseLabel:false,
      labelStyle :[styles.labelStyle],
    }
  }
);
Related