Dieguinho
Dieguinho

Reputation: 788

React Navigation: Is it possible to navigate to the same screen from different stackNavigators?

I'm coding an app that uses react-navigation. My app has a bottom TabNavigator in which each tab consists of a stackNavigator, like so:

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

Each stackNavigator inside the tabs in the tabNavigator has its own header so that I can customize the header for each tab, but all headers have a button which should navigate to the profile screen (in this example its the contact icon).

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

What I want to do is that by pressing the contact icon the app should navigate to the profile screen, what I don't know is if its possible to define like a global route thats accesible from everywhere, Or do I have to add the profile screen to all the stackNavigators so that it is accesible from every screen in every stack?

Thanks in advance!

Upvotes: 1

Views: 1875

Answers (1)

Dieguinho
Dieguinho

Reputation: 788

Found the answer here https://stackoverflow.com/a/50701940/1276438

Use a stackNavigator with the profile stack and the tabNavigator as children, making the tabNavigator the default route.

Upvotes: 1

Related Questions