6

Intro

Just like Facebook, Instagram, and any other mobile app, I want to implement 'go back to initial screen in Stacknavigator'

  1. if user press the button,
  2. it goes back to the very first page.

Simple Use Case

  1. see TabNavigator
  2. Goes to 'Feed' Tab
  3. Goes to 'User' Screen
  4. Goes to another 'User' Screen
  5. PRESS the Main Tab Icon - 'Feed'}
  6. Goes BACK to 'Feed' Tab // so you won't see the 'back' button

And please leave a comment if you don't understand this use case, I will draw its state flow for you

Code for the icon on my Main TabNavigator.

navigationOptions: ({ navigation }) => ({
      header: null,
      tabBarIcon: ({ focused }) => {
          const { routeName } = navigation.state;
          ....
          return (
              <TochableWithoutFeedback onPress={()=>{navigation.goback(iconName)}> 
              // undefined is not a function when I press the Button 
              // deeper screen. (not getting any error on Main)
                  <Ionicons
                      name={iconName}
                      size={30}
                      style={{ marginBottom: -3 }}
                      color={focused ? Colors.tabIconSelected : Colors.tabIconDefault}
                      />
             <TochableWithoutFeedback>
         );
  },
vmarquet
  • 2,384
  • 3
  • 25
  • 39
merry-go-round
  • 4,533
  • 10
  • 54
  • 102
  • Related: https://stackoverflow.com/questions/51991396/resetting-the-navigation-stack-when-changing-tab-stacknavigators-nested-in-tabn – vmarquet Aug 26 '18 at 21:10

1 Answers1

8

actually, it depends on how many deep is your routing like Instagram 2 to 3 deep routing else are tabs

so you can reset your router or go back to the main router by using

this.props.navigation.goBack(null)

for eg.

Tab navigation child ahs Stack navigation so in Stack, you can do something like

// Anyone watching this, please check your react-navigation version
// I'm using ^1.0.0-beta.21.
// props for tabBarOnpress varies can be varied (Editor John Baek)
tabBarOnPress: ({scene, jumpToIndex}) => {
      jumpToIndex(scene.index)
      navigation.dispatch(NavigationActions.reset({
        index: 0,
        actions: [
          NavigationActions.navigate({ routeName: 'home' }) // go to first screen of the StackNavigator
        ]
      }))
    }

so whenever someone press Home Tab then all route reset and you see Feed screen just like Instagram

TabNavigation 
  -Home
      |
      StakeNavigation 
                    |
                    mainScreen //main of home 
                    Subrouts 
                    RouteToProfile //just like insta

  -Search 
        |
         StakeNavigation 
                       |
                       mainScreen //main of search 
                       searchResult //if people serch some specific 

and go on... so reset route at stakeNavigation level of Tab

const SubHome = StackNavigator({
  Home: { screen: Home },
  Home2: { screen: Home2 },
  Home3 : { screen: Home3 },
},{
  navigationOptions:({navigation,screenProps})=>({
    tabBarLabel: I18n.t('tab_car'),
    tabBarIcon: ({ tintColor }) => (
      <CustomIcon name={'Home'} width={28} height={30} fill={tintColor} />
    ),
    tabBarOnPress: (tab, jumpToIndex) => {
      jumpToIndex(tab.index)
      navigation.dispatch(NavigationActions.reset({
        index: 0,
        actions: [
          NavigationActions.navigate({ routeName: 'Home' }) // go to first screen of the StackNavigator
        ]
      }))
    }
  }),
  headerMode:'none'
});
merry-go-round
  • 4,533
  • 10
  • 54
  • 102
Jigar
  • 1,314
  • 9
  • 19
  • This post explains how to call `navigation.dispatch` from `navigationOptions` . https://stackoverflow.com/a/45098153/681830 – Val Dec 07 '17 at 06:43
  • my code is really live for my application dont just everything its just for example use only TabBarPress – Jigar Dec 07 '17 at 06:51
  • avigationBar disappear coz of headerMode:'none' – Jigar Dec 07 '17 at 06:53
  • umm.. one more twist i have this issue in my application so i found solution here "react-navigation": "https://github.com/react-community/react-navigation.git#ee2e27c24bc1f0b2d5076ffe8385b39943de7e65", – Jigar Dec 07 '17 at 06:54
  • 1
    i am using specific version of navigation for tab click – Jigar Dec 07 '17 at 06:54
  • yeap. upgrading react-navigation to beta.21 solved the problem. Now it says `undefined is not a function. (jumpToIndex(tab.index)`. I don't understand why it's `undefined`. – merry-go-round Dec 07 '17 at 07:03
  • use this version https://github.com/react-community/react-navigation.git#ee2e27c24bc1f0b2d5076ffe8385b39943de7e65 – Jigar Dec 07 '17 at 07:09
  • I'm struggling with these problems! Please help!!! https://stackoverflow.com/questions/48050248/how-to-dispatch-multiple-actions-in-react-navigation – merry-go-round Jan 01 '18 at 13:59
  • AND https://stackoverflow.com/questions/47930049/how-to-reset-all-screens-when-user-logs-out-scroll-and-page-index – merry-go-round Jan 01 '18 at 13:59
  • I think you can solve this problem (+200 bounty) : https://stackoverflow.com/questions/47930049/how-to-reset-tabnavigator-when-user-logs-out-from-other-screen – merry-go-round Jan 03 '18 at 05:24