简体   繁体   中英

How to know which screen is focused at screen level in React Native?

I would like to know how I could retrieve the information about which tab is focused at a screen level with React Navigation when using createBottomTabNavigator . With focused , it's possible to know if the tab is focused or not but if not we don't know which one is focused. A pseudo code of my need would be like something like this (currentRoute doesn't exist):

 Homepage: {
    screen: HomeNavigator,
    navigationOptions: {
      tabBarIcon: ({ focused }) => {
        if (focused) {
          return <Icon source={homeLogoOn} />
        } else {
          if (currentRoute === 'Me') {
            return <Icon source={homeLogoOffLeft} />
          } else if (currentRoute === 'MyFriends') {
            return <Icon source={homeLogoOffRight} />
          }
        }
      },
      tabBarLabel: <View />,
    },

react-navigation exposes an HOC to know if the current screen is focussed.

You can check the docs about it here: https://reactnavigation.org/docs/en/with-navigation-focus.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM