React-Navigation版本5中的`tabBarComponent`选项在哪里?

问题描述 投票:0回答:1

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

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

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

react-native react-navigation react-navigation-bottom-tab
1个回答
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

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

© www.soinside.com 2019 - 2024. All rights reserved.