Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Open Stack.Tab from a Stack.Screen contained within a tab with different NavigationContainers in React Native.

Tiempo de lectura: 2 minutos

Reading time: 3 minutes

I’m going to explain how to open another tab within a screen (outside the tab but within the execution stack) that already has content in our tab.

For example, we have the following screen and we want to perform the navigation shown in the video:

And we want to open the “Buscar” tab using the “Click here to search” button, which is contained within a different screen and added to the “Mis Cursos” tab.

The code is as follows:

MenuTab.js

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import React from "react";
import { View, StyleSheet, Text, Button } from "react-native";
import { NavigationContainer } from '@react-navigation/native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import MisCursos from '../screens/MenuMisCursos';
import MenuBuscar from '../screens/MenuBuscar';
//Tab navigator
const Tab = createBottomTabNavigator();
//Button component
const Menu = ({ navigation }) => {
return (
<View style={[styles.contenedorTotal]}>
<NavigationContainer style={[styles.contanierSup]} independent={true} >
<Tab.Navigator initialRouteName="Mis cursos">
<Tab.Screen name="Buscar" component={MenuBuscar} />
<Tab.Screen name="Mis cursos" component={MisCursos} />
/>
</Tab.Navigator>
</NavigationContainer>
</View >
)
};
export default Menu;
import React from "react"; import { View, StyleSheet, Text, Button } from "react-native"; import { NavigationContainer } from '@react-navigation/native'; import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; import MisCursos from '../screens/MenuMisCursos'; import MenuBuscar from '../screens/MenuBuscar'; //Tab navigator const Tab = createBottomTabNavigator(); //Button component const Menu = ({ navigation }) => { return ( <View style={[styles.contenedorTotal]}> <NavigationContainer style={[styles.contanierSup]} independent={true} > <Tab.Navigator initialRouteName="Mis cursos"> <Tab.Screen name="Buscar" component={MenuBuscar} /> <Tab.Screen name="Mis cursos" component={MisCursos} /> /> </Tab.Navigator> </NavigationContainer> </View > ) }; export default Menu;
import React from "react";
import { View, StyleSheet, Text, Button } from "react-native";
import { NavigationContainer } from '@react-navigation/native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';

import MisCursos from '../screens/MenuMisCursos';
import MenuBuscar from '../screens/MenuBuscar';

//Tab navigator
const Tab = createBottomTabNavigator();

//Button component
const Menu = ({ navigation }) => {
    return (
        <View style={[styles.contenedorTotal]}>
            <NavigationContainer style={[styles.contanierSup]} independent={true} >
                <Tab.Navigator initialRouteName="Mis cursos">
                    <Tab.Screen name="Buscar" component={MenuBuscar} />
                    <Tab.Screen name="Mis cursos" component={MisCursos} />
                   />
                </Tab.Navigator>
            </NavigationContainer>
        </View >
    )
};

export default Menu;

As you can see in this code, we have only created a bottom tab navigator.

Now we are going to create the Mis Cursos screen, which in turn will contain another NavigationContainer that will display one screen or another as appropriate.

MenuMisCursos.js

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import React, { useState, useEffect } from "react";
import { View, StyleSheet } from "react-native";
import { NavigationContainer, NavigationActions } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
//Import screens
import MisCursos from './MisCursos';
import Curso from './Curso';
const Stack = createNativeStackNavigator();
//Create a component called MenuMisCursos
const MenuMisCursos = ({ navigation }) => {
return (
<View style={styles.container}>
<NavigationContainer independent={true} >
<Stack.Navigator initialRouteName="Mis cursos">
<Stack.Screen name="Mis Cursos" component={MisCursos} initialParams={{ navigationTab: { navigation } }} options={{ headerShown: false }} />
<Stack.Screen name="Curso" component={Curso} options={{ headerShown: false }} />
</Stack.Navigator>
</NavigationContainer>
</View>
)
};
//Export the component
export default MenuMisCursos;
const styles = StyleSheet.create({
container: {
flex: 1,
}
});
import React, { useState, useEffect } from "react"; import { View, StyleSheet } from "react-native"; import { NavigationContainer, NavigationActions } from '@react-navigation/native'; import { createNativeStackNavigator } from '@react-navigation/native-stack'; //Import screens import MisCursos from './MisCursos'; import Curso from './Curso'; const Stack = createNativeStackNavigator(); //Create a component called MenuMisCursos const MenuMisCursos = ({ navigation }) => { return ( <View style={styles.container}> <NavigationContainer independent={true} > <Stack.Navigator initialRouteName="Mis cursos"> <Stack.Screen name="Mis Cursos" component={MisCursos} initialParams={{ navigationTab: { navigation } }} options={{ headerShown: false }} /> <Stack.Screen name="Curso" component={Curso} options={{ headerShown: false }} /> </Stack.Navigator> </NavigationContainer> </View> ) }; //Export the component export default MenuMisCursos; const styles = StyleSheet.create({ container: { flex: 1, } });
import React, { useState, useEffect } from "react";
import { View, StyleSheet } from "react-native";
import { NavigationContainer, NavigationActions } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';

//Import screens
import MisCursos from './MisCursos';
import Curso from './Curso';

const Stack = createNativeStackNavigator();

//Create a component called MenuMisCursos
const MenuMisCursos = ({ navigation }) => {

    return (
        <View style={styles.container}>
            <NavigationContainer independent={true} >
                <Stack.Navigator initialRouteName="Mis cursos">

                    <Stack.Screen name="Mis Cursos" component={MisCursos} initialParams={{ navigationTab: { navigation } }} options={{ headerShown: false }} />

                    <Stack.Screen name="Curso" component={Curso} options={{ headerShown: false }} />

                </Stack.Navigator>
            </NavigationContainer>

        </View>
    )
};

//Export the component
export default MenuMisCursos;

const styles = StyleSheet.create({
    container: {
        flex: 1,
    }
});

In this code, we create a NavigationContainer that contains two screens. The goal is to open the “Buscar” tab from one of these screens by pressing a button.

To do this, we first pass the navigation attribute as a parameter that comes from the Navigation.Tab:

{ navigation }
{ navigation }

This way, we pass the navigation stack of the previous screen that corresponds to the tabs.

We want to navigate within the screen we have created, so we pass it as a parameter:

initialParams={{ navigationTab: { navigation } }}
initialParams={{ navigationTab: { navigation } }}

We have created an attribute called navigationTab and passed the corresponding navigation parameter to it.

Now we just have to retrieve this attribute and use it within the corresponding button in the MisCursos.js screen:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import React, { useState } from "react";
import { View, StyleSheet, Text } from "react-native";
//Import components:
import Button from '../components/Button';
//Button component
const MisCursos = ({ navigation, route }) => {
const { navigationTab } = route.params;
return (
<View style={[styles.contenedorInicio]}>
<View style={[styles.contenedorInicio]}>
<Text style={[styles.texto]}>You don't have any courses</Text>
<Button text="Click here to search" onPress={() => navigationTab.navigation.navigate('Buscar')} />
</View>
</View>
)
};
export default MisCursos;
const styles = StyleSheet.create({
texto: {
fontSize: 30,
color: '#000',
},
imagen:
{
width: 300,
height: 200,
},
padding: {
paddingBottom: 10,
paddingTop: 10,
},
contenedorInicio: {
flex: 1,
display: 'flex',
alignContent: 'center',
alignItems: 'center',
}
});
import React, { useState } from "react"; import { View, StyleSheet, Text } from "react-native"; //Import components: import Button from '../components/Button'; //Button component const MisCursos = ({ navigation, route }) => { const { navigationTab } = route.params; return ( <View style={[styles.contenedorInicio]}> <View style={[styles.contenedorInicio]}> <Text style={[styles.texto]}>You don't have any courses</Text> <Button text="Click here to search" onPress={() => navigationTab.navigation.navigate('Buscar')} /> </View> </View> ) }; export default MisCursos; const styles = StyleSheet.create({ texto: { fontSize: 30, color: '#000', }, imagen: { width: 300, height: 200, }, padding: { paddingBottom: 10, paddingTop: 10, }, contenedorInicio: { flex: 1, display: 'flex', alignContent: 'center', alignItems: 'center', } });
import React, { useState } from "react";
import { View, StyleSheet, Text } from "react-native";

//Import components:
import Button from '../components/Button';

//Button component
const MisCursos = ({ navigation, route }) => {

    const { navigationTab } = route.params;
   
   
    return (
        <View style={[styles.contenedorInicio]}>
           
                <View style={[styles.contenedorInicio]}>
                    <Text style={[styles.texto]}>You don't have any courses</Text>
                    <Button text="Click here to search" onPress={() =>  navigationTab.navigation.navigate('Buscar')} />
                </View>
           
        </View>
    )
};

export default MisCursos;

const styles = StyleSheet.create({
    texto: {
        fontSize: 30,
        color: '#000',
    },
    imagen:
    {
        width: 300,
        height: 200,
    },
    padding: {
        paddingBottom: 10,
        paddingTop: 10,
    },
    contenedorInicio: {
        flex: 1,
        display: 'flex',
        alignContent: 'center',
        alignItems: 'center',
    }
});

To receive the parameter we want to navigate to, it is passed in the render function as “route”:

const MisCursos = ({ navigation, route }) => {
const MisCursos = ({ navigation, route }) => {

*Do not confuse navigation with the previous object, as this navigation belongs to the Stack.Screen of the MenuMisCursos.js screen. The one we are interested in is within route, as we have passed it as initialParams.

0

Leave a Comment