【问题标题】:Where is the `tabBarComponent` option in React-Navigation version 5?React-Navigation 版本 5 中的 `tabBarComponent` 选项在哪里?
【发布时间】:2020-01-24 14:34:42
【问题描述】:

我正在将 RN 项目版本 4 迁移到 5。

我使用tabBarComponent 选项将标签栏组件替换为自定义组件。 Old docs.

如何在版本5中实现相同,我在new docs中找不到这个选项。

【问题讨论】:

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


    【解决方案1】:

    这是实现自定义标签组件的新 API 方式:

    import { View, Text, TouchableOpacity } from 'react-native';
    
    function MyTabBar({ state, descriptors, navigation }) {
      return (
        <View style={{ flexDirection: 'row' }}>
          {state.routes.map((route, index) => {
            const { options } = descriptors[route.key];
            const label =
              options.tabBarLabel !== undefined
                ? options.tabBarLabel
                : options.title !== undefined
                ? options.title
                : route.name;
    
            const isFocused = state.index === index;
    
            const onPress = () => {
              const event = navigation.emit({
                type: 'tabPress',
                target: route.key,
              });
    
              if (!isFocused && !event.defaultPrevented) {
                navigation.navigate(route.name);
              }
            };
    
            const onLongPress = () => {
              navigation.emit({
                type: 'tabLongPress',
                target: route.key,
              });
            };
    
            return (
              <TouchableOpacity
                accessibilityRole="button"
                accessibilityStates={isFocused ? ['selected'] : []}
                accessibilityLabel={options.tabBarAccessibilityLabel}
                testID={options.tabBarTestID}
                onPress={onPress}
                onLongPress={onLongPress}
                style={{ flex: 1 }}
              >
                <Text style={{ color: isFocused ? '#673ab7' : '#222' }}>
                  {label}
                </Text>
              </TouchableOpacity>
            );
          })}
        </View>
      );
    }
    
    // ...
    
    <Tab.Navigator tabBar={props => <MyTabBar {...props} />}>
      {...}
    </Tab.Navigator>
    

    此链接肯定会有所帮助。 https://reactnavigation.org/docs/en/next/bottom-tab-navigator.html#tabbar

    希望这会有所帮助。干杯!

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 2021-03-04
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    相关资源
    最近更新 更多