首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React-Navigation版本5中的`tabBarComponent`选项在哪里?

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

Stack Overflow用户
提问于 2020-01-24 22:34:42
回答 1查看 1.1K关注 0票数 0

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

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-24 22:42:24

下面是实现自定义选项卡组件的新API方法:

代码语言:javascript
复制
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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59898517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档