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
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
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 }
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 } }}
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:
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 }) => {
*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.