【问题标题】:How do I get navigation and state in bottom tab如何在底部选项卡中获取导航和状态
【发布时间】:2021-12-07 05:08:49
【问题描述】:

我正在使用react native bottom navigation 6。我想获取导航和状态,但它返回未定义。

export default function Navigation() {
  return (
    <NavigationContainer linking={LinkingConfiguration}>
      <RootNavigator />
    </NavigationContainer>
  );
}


const Stack = createNativeStackNavigator<RootStackParamList>();

function RootNavigator() {
  return (
    <Stack.Navigator>
      <Stack.Screen name="Login" component={LoginScreen} options={{ headerShown: false }} />
      <Stack.Screen name="Root" component={BottomTabBar} options={{ headerShown: false }} />
    </Stack.Navigator>
  );
}

/**
 * A bottom tab navigator displays tab buttons on the bottom of the display to switch screens.
 * https://reactnavigation.org/docs/bottom-tab-navigator
 */

这里返回 null ({ navigation, state,descriptors }: BottomTabBarProp) 我想使用 state.index 来获取当前选项卡并显示不同的图标/svg。我的替代方法是使用专注。

const BottomTab = createBottomTabNavigator<RootTabParamList>();

const BottomTabBar = ({ navigation, state,descriptors }: BottomTabBarProps) => (
  <>
  <BottomTab.Navigator 
  initialRouteName="Feed"
  screenOptions={{
    tabBarShowLabel: false,
    tabBarActiveTintColor: Colors.activeNavIcon,
    tabBarInactiveTintColor: Colors.inactiveNavIcon
  }}
>
  <BottomTab.Screen
    name="Feed"
    component={FeedNavigator}
    options={{
      tabBarIcon: ({ color, size, focused }) => (
        focused ?
          <HomeActive width={size} height={size} color={color} />
          : <Home width={size} height={size} color={color} />
      ),
    }}
  />

 
  <BottomTab.Screen
    name="Profile"
    component={ProfileNavigator}
    options={{
      tabBarIcon: ({ color, size, focused  }) => (
        focused ?
        <ProfileActive width={size} height={size} color={color} />
        : <Profile width={size} height={size} color={color} />
      ),
    }}
  />
</BottomTab.Navigator>
  </>
);

【问题讨论】:

  • 您正在将导航器呈现为底部标签栏,或者您要做什么?在常规组件中期望BottomTabBarProps 很奇怪。它们仅在标签导航器的tabBar 属性中指定的自定义底部标签栏中可用。
  • "显示不同的图标/svg" 你想在哪里显示不同的图标?
  • 只想使用导航和状态。如果 state==0 ?:

标签: react-native react-navigation react-navigation-stack react-navigation-bottom-tab react-navigation-v6


【解决方案1】:

当您使用功能组件时。我推荐使用react-navigation提供的Hooks。

使用导航

Link to Docs

useNavigationState

Link to Docs

const BottomTabBar = () => {

   const navigation = useNavigation()
  
   return (
     <>
     <BottomTab.Navigator 
       initialRouteName="Feed"
       screenOptions={{
         tabBarShowLabel: false,
         tabBarActiveTintColor: Colors.activeNavIcon,
         tabBarInactiveTintColor: Colors.inactiveNavIcon
       }}
     />
  ...............
  ...............
  ..............
>
  )
}

【讨论】:

  • 您好,感谢您的回答。我很困惑我在哪里放 const routesIndex = useNavigationState(state => state.index);我得到钩子错误
  • @VincentDaveNavaresTe 我已经更新了我的答案,请检查一下。如果它解决了您的问题,请点赞并接受我的回答
  • 我正在使用这个 const routesIndex = useNavigationState(state => state.index);但由于某种原因,它总是返回 1。想知道哪个标签当前处于活动状态
  • 获取堆栈状态而不是底部导航。任何想法如何获得底部导航状态?
  • 不能从同一个组件获取BottomNav的State,只能从其子组件获取BottomState。
猜你喜欢
  • 1970-01-01
  • 2020-08-26
  • 2020-09-10
  • 1970-01-01
  • 2018-11-18
  • 1970-01-01
  • 2021-01-30
  • 1970-01-01
  • 2016-06-29
相关资源
最近更新 更多