Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to check if a specific bottom tab is active before showing another component?

I'm using react navigation 5 and used createBottomTabNavigator() to create a bottom tab to render the body with different components when tapped. The problem now is that I also need to show a text component only when 'Settings' tab is active and it's showing the Settings component. Is there a way to check which bottom tab is active?

I know for navigation routes there's useRoutes is there something similar?

    <Tab.Navigator
        initialRouteName="Home"
        tabBarOptions={{
          inactiveTintColor: theme.inactive,
          activeTintColor: theme.active,
        }}>
        <Tab.Screen
          name="Home"
          options={{
            tabBarIcon: ({color}) => <HomeIcon color={color} />,
          }}>
          {() => (
            <Home content={feed} />
          )}
        </Tab.Screen>
        <Tab.Screen
          name="Settings"
          component={Settings}
          options={{
            tabBarIcon: ({color}) => <SettingsIcon color={color} />,
          }}
        />
      </Tab.Navigator>

i.e. route.name === "Settings" ? this : that

like image 351
zosozo Avatar asked Dec 12 '25 03:12

zosozo


2 Answers

options={{
        tabBarIcon: ({focused}) => <SettingsIcon color={focused ? 'red' : 'blue'} />,
      }}

you could use props focused. It return True if your tab focused. Opposite, it return false

like image 136
meowww Avatar answered Dec 14 '25 15:12

meowww


The useFocusEffect() hook provided by React Navigation is the answer to my question.

https://reactnavigation.org/docs/use-focus-effect/

like image 45
zosozo Avatar answered Dec 14 '25 15:12

zosozo