Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show TabBar label only on active Tab?

I would like to hide the TabBar Label when the tab is not active. With showLabel from tabBarOptions I can only disable them completely. Thanks in advance.

Material Design TabBar

like image 540
Defrian Avatar asked Feb 27 '18 16:02

Defrian


2 Answers

Hey Drew thanks for the idea, couldn't figure it out on my own, but I think you have a lot of extra code unnecessary for the functionality asked in the question. Here is my take on this, and this works just as well.

export const BottomTabNavigator = createBottomTabNavigator(
  {
    News: {
      screen: NewsNavigator,
      navigationOptions: {
        tabBarIcon: ({ focused }: any) =>
          focused ? <NewsIconActive /> : <NewsIcon />
      }
    },
    Rewards: {
      screen: RewardsNavigator,
      navigationOptions: {
        tabBarIcon: ({ focused }: any) =>
          focused ? <RewardsIconActive /> : <RewardsIcon />
      }
    },
    Home: {
      screen: HomeNavigator,
      navigationOptions: {
        tabBarIcon: ({ focused }: any) =>
          focused ? <HomeIconActive /> : <HomeIcon />
      }
    },
    Leaderboard: {
      screen: LeaderboardNavigator,
      navigationOptions: {
        tabBarIcon: ({ focused }: any) =>
          focused ? <LeaderboardIconActive /> : <LeaderboardIcon />
      }
    },
    Profile: {
      screen: ProfileNavigator,
      navigationOptions: {
        tabBarIcon: ({ focused }: any) =>
          focused ? <ProfileIconActive /> : <ProfileIcon />
      }
    }
  },
  {
    initialRouteName: 'Profile'
    },
    navigationOptions: ({ navigation }) => ({
      tabBarLabel: ({ focused }) => {
        const { routeName } = navigation.state;
        switch (routeName) {
          case 'News':
            return focused ? (
              <Text>{routeName}</Text>
            ) : null;
            break;
          case 'Rewards':
            return focused ? (
              <Text>{routeName}</Text>
            ) : null;
            break;
          case 'Home':
            return focused ? (
              <Text>{routeName}</Text>
            ) : null;
            break;
          case 'Leaderboard':
            return focused ? (
              <Text >{routeName}</Text>
            ) : null;
            break;
          case 'Profile':
            return focused ? (
              <Text>{routeName}</Text>
            ) : null;
            break;
          default:
            return null;
            break;
        }
      }
    })
  }
);
like image 119
Sebastijan Dumančić Avatar answered Sep 29 '22 13:09

Sebastijan Dumančić


This fairly simple solution worked for me in version 5 of React Navigation:

<Tab.Navigator
    screenOptions={({ route, navigation }) => {
        return { tabBarLabel: navigation.isFocused() ? route.name : '' };
    }}
>
like image 22
Isaac Avatar answered Sep 29 '22 12:09

Isaac