javascriptreact-nativereact-navigation

React Navigation how to hide tabbar from inside stack navigation


I have the following stack navigation and screens:

export const HomeStack = createStackNavigator({
    Home: HomeScreen,
    Categories: CategoriesScreen,
    Products: ProductsScreen,
    ProductDetails: ProductDetailsScreen,
})

I want to hide tabs only in ProductDetailsScreen:

export const hideTabBarComponents = [
    'ProductDetails',
]

export const MainTabs = createBottomTabNavigator(
    {
        Home: HomeStack,
        Favorite: FavoriteScreen,
        Account: AccountScreen,
        Help: HelpScreen,
        Events: EventsScreen
    },
    {
        navigationOptions: ({ navigation }) => ({

            tabBarIcon: ({ focused, tintColor }) => {
                ...
            },
            tabBarLabel: ({ focused, tintColor }) => {
                ...
            },
            
            tabBarVisible: ! hideTabBarComponents.includes(navigation.state.routeName)

        }),
    }
);

The problem is that can't pass any options to Tab navigation from Stack Navigation

Not all of the stack screens only one of them


Solution

  • To hide the tab bar in one of the screens, this works for React Navigation v4:

    HomeStack.navigationOptions = ({ navigation }) => {
    
        let tabBarVisible = true;
    
        let routeName = navigation.state.routes[navigation.state.index].routeName
    
        if ( routeName == 'ProductDetails' ) {
            tabBarVisible = false
        }
    
        return {
            tabBarVisible,
        }
    }
    

    For v5, and v6 please check @Chathuranga Kasthuriarachchi's answer here