反应导航:是否可以从不同的stackNavigators导航到同一屏幕?

时间:2018-10-10 00:08:54

标签: react-native react-navigation react-navigation-stack

我正在编写一个使用反应导航的应用程序。我的应用程序有一个底部的TabNavigator,其中每个选项卡都包含一个stackNavigator,如下所示:

Routes.js:

const FirstStack = createStackNavigator({
    First: {
        screen: FirstScreen,
        navigationOptions: {
            header: props => <AppToolbar />
        }
    }
});

const SecondStack = createStackNavigator({
    Second: {
        screen: SecondScreen,
        navigationOptions: {
            header: props => <AppToolbar />
        }
    }
});

const ThirdStack = createStackNavigator({
    Third: {
        screen: ThirdScreen,
        navigationOptions: {
            header: props => <AppToolbar />
        }
    }
});

const FourthStack = createStackNavigator({
    Fourth: {
        screen: FourthScreen,
        navigationOptions: {
            header: props => <AppToolbar />
        }
    }
});

const FifthStack = createStackNavigator({
    Fifth: {
        screen: FifthScreen,
        navigationOptions: {
            header: props => <AppToolbar />
        }
    }
});

export const AppStack = createBottomTabNavigator(
    {
        First: {
            screen: FirstStack,
            navigationOptions: {
                title: "First",
                tabBarLabel: "First",
                tabBarIcon: ({ tintColor }) => (
                    <Icon name="ios-home" color={tintColor} size={24} />
                )
            }
        },
        Second: {
            screen: SecondStack,
            navigationOptions: {
                title: "Second",
                tabBarLabel: "Second",
                tabBarIcon: ({ tintColor }) => (
                    <Icon name="ios-clock" color={tintColor} size={24} />
                )
            }
        },
        Third: {
            screen: ThirdStack,
            navigationOptions: {
                title: "Third",
                tabBarLabel: "Third",
                tabBarIcon: ({ tintColor }) => (
                    <Icon name="ios-fitness" color={tintColor} size={24} />
                )
            }
        },
        Fourth: {
            screen: FourthStack,
            navigationOptions: {
                title: "Fourth",
                tabBarLabel: "Fourth",
                tabBarIcon: ({ tintColor }) => (
                    <Icon
                        name="ios-cloud-download"
                        color={tintColor}
                        size={24}
                    />
                )
            }
        },
        Fifth: {
            screen: FifthStack,
            navigationOptions: {
                title: "Fifth",
                tabBarLabel: "Fifth",
                tabBarIcon: ({ tintColor }) => (
                    <Icon name="ios-person" color={tintColor} size={24} />
                )
            }
        }
    },
    {
        initialRouteName: "First",
        order: ["First", "Second", "Third", "Fourth", "Fifth"],
        tabBarOptions: {
            activeTintColor: "white",
            inactiveTintColor: "grey",
            style: {
                backgroundColor: "#121212",
                borderTopColor: "#303030"
            }
        }
    }
);

tabNavigator的选项卡中的每个stackNavigator都有其自己的标题,以便我可以自定义每个选项卡的标题,但是所有标题都具有一个按钮,该按钮应导航到配置文件屏幕(在本示例中为联系人图标)。 / p>

AppToolbar.js:

const appToolbar = props => {
    return (
        <View style={styles.toolbar}>
            <Text style={styles.toolbarTitle}>Title</Text>
            <TouchableOpacity onPress={...}>
                <Icon
                    name="ios-contact"
                    color="grey"
                    size={30}
                    style={{ padding: 0, margin: 0, marginRight: 10 }}
                />
            </TouchableOpacity>
        </View>
    );
};

我想做的是,通过按联系人图标,应用程序应导航到配置文件屏幕,我不知道是否有可能像在任何地方都可以访问的那样定义一条全球路线,或者我必须将配置文件屏幕添加到所有stackNavigators中,以便可以在每个堆栈中的每个屏幕上使用它吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

在这里https://stackoverflow.com/a/50701940/1276438

找到了答案

使用带有配置文件堆栈和tabNavigator作为子项的stackNavigator,使tabNavigator成为默认路由。