I am building an app using react-native 0.64 and react-navigation v5.
I am trying to achieve a navigation structure with a Drawer on top, a HomeScreen (WITHOUT bottom tabs) with buttons to navigate to 5 screens -those WITH bottom tabs- and a BottomTab with 6 tabs: same 5 screens (each of them allows to navigate to a different sub-screen) and a tab for the HomeScreen.
According to react-navigation docs (Hiding tab bar in specific screens), in my implementation I avoid to use 'tabBarVisible' prop and instead I put HomeScreen in a parent Stack while the BottomTab is one of its screens. The problem is I would like to have in the BottomTab a tab for HomeScreen even if this screen is not inside the BottomTab (when the user press on the Home tab the app should navigate to the HomeScreen where the BottomTab won't be present anymore).
What I tried to implement is a structure like this:
- [Drawer]
- [MainStack]
- HomeScreen
- [BottomTab]
- HomeScreen (with listener prop that on 'tabPress' => navigates back to HomeScreen)
- [Stack1]
- Screen1
- DetailScreen1
- [Stack2]
- Screen2
- DetailScreen2
- . . . (other 3 stacks)
- [MainStack]
QUESTIONS:
Is the proper way to nesting/configuring the navigators to obtain the expected final result? Is it 'safe' and correct to have HomeScreen as component for screens on parent Stack and child BottomTab too?
It seems to be a heavy solution (5 Stacks inside a BottomTab inside a Stack inside a Drawer and soon I will add another stack for authorization screens), that slow down running the app: is there a more efficient way to organize navigators using pre-built navigators or else building ie. a custom component for tabBar so that possibly all screens are inside less stack (one?) or anything else that could help me to build lighter solution?
I published on expo.io the full (and simple) sample code implementation, below is the partial code:
const DrawerNav = createDrawerNavigator();
const Drawer = ({ navigation }) => {
return (
<DrawerNav.Navigator>
<DrawerNav.Screen name="MainStack" component={MainStack} />
<DrawerNav.Screen name="OtherScreen" component={OtherScreen} />
</DrawerNav.Navigator>
);
};
const MainStackNav = createStackNavigator();
const MainStack = ({ navigation }) => {
return (
<MainStackNav.Navigator headerMode="none">
<MainStackNav.Screen name="MainHome" component={HomeScreen} />
<MainStackNav.Screen name="MainBottomTab" component={BottomTab} />
</MainStackNav.Navigator>
);
};
const BottomTabNav = createBottomTabNavigator();
const BottomTab = ({ navigation }) => {
return (
<BottomTabNav.Navigator>
<BottomTabNav.Screen name="Home" component={HomeScreen}
listeners={({ navigation }) => ({
tabPress: (e) => {
e.preventDefault();
navigation.popToTop();
},
})}/>
<BottomTabNav.Screen name="Stack1" component={Stack1} />
<BottomTabNav.Screen name="Stack2" component={Stack2} />
<BottomTabNav.Screen name="Stack3" component={Stack3} />
<BottomTabNav.Screen name="Stack4" component={Stack4} />
<BottomTabNav.Screen name="Stack5" component={Stack5} />
</BottomTabNav.Navigator>
);
};