How to set a default screen Route in react Tab Navigation in React Native

theCoder picture theCoder · Apr 8, 2019 · Viewed 8.2k times · Source

I want to load dashboard to be active tab in react Native bottom tab. Navigation whenever dashboard is loaded but whenever I move to dashboard it moves to inbox screen which is the first element in my react Bottom Tab Navigation. Is there any way to create a default screen whenever screen bottom tabs is used?

The code I am using for bottom navigation is

 dashboard: {
        screen: createBottomTabNavigator({
            inbox: {
                screen: Chat,
                navigationOptions: ({ navigation }) => ({
                    title: 'Inbox',
                }),
            },
            favourite: {
                screen: Favourite,
                navigationOptions: ({ navigation }) => ({
                    title: 'Favourite',
                }),
            },
            dashboard: {
                screen: Dashboard,
                navigationOptions: ({ navigation }) => ({
                    title: 'Home',
                    initialRouteName: 'dashboard'
                }),
            },
            setting: {
                screen: SettingScreen,
                navigationOptions: ({ navigation }) => ({
                    title: 'Setting',
                }),
            },
            survey: {
                screen: NutritionistSurvey,
                navigationOptions: ({ navigation }) => ({
                    title: 'Survey',
                }),
            },
        }),
        navigationOptions: ({ navigation }) => ({
            title: 'Dashboard',

        }),


    },

Even though the navigation works completely fine I just need a way to load dashboard screen whenever the user navigates to Dashboard.

Answer

Vishal Dhanotiya picture Vishal Dhanotiya · Apr 8, 2019

You can add initialRouteName in createBottomTabNavigator I will solve your problem

 const MyApp = createBottomTabNavigator(
      {
        Inbox: {
          screen: Chat,
          navigationOptions: ({ navigation }) => ({
            title: "Inbox"
          })
        },
        Favourite: {
          screen: Favourite,
          navigationOptions: ({ navigation }) => ({
            title: "Favourite"
          })
        },
        Dashboard: {
          screen: Dashboard,
          navigationOptions: ({ navigation }) => ({
            title: "Home"
          })
        }
      },
      {
        initialRouteName: "Dashboard"
      }
    );