如何在React Native中的反应选项卡导航中设置默认屏幕路由

问题描述 投票:0回答:2

我想在响应Native底部选项卡中加载仪表板作为活动选项卡。加载仪表板时导航,但每当我移动到仪表板时,它都会移动到收件箱屏幕,这是我的反应底部选项卡导航中的第一个元素。每当使用屏幕底部标签时,有没有办法创建默认屏幕?

我用于底部导航的代码是

 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',

        }),


    },

即使导航工作完全正常,我只需要在用户导航到仪表板时加载仪表板屏幕。

react-native native react-navigation react-navigation-stack
2个回答
2
投票

您可以在createBottomTabNavigator中添加initialRouteName我将解决您的问题

 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"
      }
    );

0
投票

您有2个名为dashboard的屏幕,bottomTabNavigator和Dashboard屏幕,因此当您尝试导航到仪表板时,react-navigation将导航到bottomTabNavigator的第一个选项卡。

重命名bottomTabNavigator,它应该工作:

 dashboardTabs: {
        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',
        }),
    },
© www.soinside.com 2019 - 2024. All rights reserved.