I have a problem with bottom tab navigation. This is screen structure:
- Main Tab:
- Tab A
- Screen A1
- Tab B
- Screen B1
- Screen B2
- Tab A
I can navigate from Screen A1 to Screen B2 (changing tab). I can navigate from Screen B1 to Screen B2.
This is the example code:
import React from "react";
import { StyleSheet, View, Text, Button } from "react-native";
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
const Tab = createBottomTabNavigator();
const Stack = createStackNavigator();
const TabAScreen1 = ({ route, navigation }) => {
return (
<View>
<Text>Tab A screen 1</Text>
<Button
title="Go to tab B screen 2"
onPress={() => {
navigation.navigate("TabBStack", { screen: "TabBScreen2" });
}}
/>
</View>
);
};
const TabBScreen1 = ({ route, navigation }) => {
return (
<View>
<Text>Tab B screen 1</Text>
<Button
title="Go to tab B screen 2"
onPress={() => {
navigation.navigate("TabBScreen2");
}}
/>
</View>
);
};
const TabBScreen2 = ({ route, navigation }) => {
return (
<View>
<Text>Tab B screen 2</Text>
</View>
);
};
function TabBStack() {
return (
<Stack.Navigator initialRouteName="TabBScreen1" screenOptions={{ headerShown: false }}>
<Stack.Screen name="TabBScreen1" component={TabBScreen1}/>
<Stack.Screen name="TabBScreen2" component={TabBScreen2}/>
</Stack.Navigator>
);
}
function MainTab() {
return (
<Tab.Navigator screenOptions={{ headerShown: false }}>
<Tab.Screen name="TabAScreen1" component={TabAScreen1}/>
<Tab.Screen name="TabBStack" component={TabBStack}/>
</Tab.Navigator>
);
};
const App = props => {
const navigationRef = useRef();
return (
<NavigationContainer>
<MainTab/>
</NavigationContainer>
);
};
const styles = StyleSheet.create({});
export default App;
Navigation packages:
"@react-navigation/bottom-tabs": "^6.2.0",
"@react-navigation/native": "^6.0.8",
"@react-navigation/stack": "^6.1.1",
At the beginning, if I navigate to Tab B using bottom tab button, Screen B1 is loaded. Then I can navigate to Screen B2 from Screen B1 or Screen A1. Being in Screen B2, if I tap on Tab B bottom tab button the Screen B2 is popped and Screen B1 is showed. This is the expected behavior.
The problem is this one. At the beginning, if I navigate to Screen B2 from Screen A1, Screen B2 is loaded. At this point, I can't navigate to screen B1 at all. If I tap on Tab B bottom tab button nothing happens, even initialRouteName
of TabBStack
is set to TabBScreen1
. I think in this case the first screen in the stack is B2.
Is it possible to pop B2 (or all screens in the stack) and push B1 tapping bottom tab button? I know about listeners, but I don't know how to implement this behavior.
function MainTab() {
return (
<Tab.Navigator screenOptions={{ headerShown: false }}>
<Tab.Screen name="TabAScreen1" component={TabAScreen1}/>
<Tab.Screen name="TabBStack" component={TabBStack}
listeners={({ navigation }) => ({
tabPress: (e) => {
console.log("Tab B pressed");
// If current route is not Screen B1, then pop all and push Screen B1
},
})}
/>
</Tab.Navigator>
);
};