【问题标题】:react navigation tab screen icon color props type反应导航选项卡屏幕图标颜色道具类型
【发布时间】:2020-10-22 23:49:05
【问题描述】:

我在我的 react native 项目中使用 eslint 和 vs 代码。

我使用 react navigation v5 创建了一个底部标签导航:

 ...
   <Tab.Screen
        name="Contacts"
        component={ContactStackScreen}
        options={{
          tabBarLabel: 'Contacts',
          tabBarColor: COLORS.DEFAULT,
          tabBarIcon: ({color}) => (
            <MaterialCommunityIcons name="contacts" color={color} size={26} />
          ),
        }}
       ...

我收到颜色道具的 eslint 错误:

道具验证中缺少'color'

我试图修复它:

ButtomTabs.propTypes = {
  color: PropTypes.string,
};

但我收到了这个错误:

propType "color" 不是必须的,但没有对应的 defaultProps 声明

【问题讨论】:

  • 也许 activeTintColorinactiveTintColor 道具 Docs
  • tabBarIcon 给定 {focused: boolean, color: string, size: number } 的函数返回一个 React.Node,显示在标签栏中。 // 在反应导航的文档中

标签: react-native eslint react-proptypes eslint-config-airbnb


【解决方案1】:

忽略警告。这是一个误报。

  1. tabBarIcon 不是组件,propTypes 仅适用于组件
  2. 您在 BottomTabs 组件上添加了 propTypes,但假设传递给 tabBarIcon 的函数是组件,则该警告可能来自 eslint 插件

【讨论】:

  • 你能告诉我一个好的 eslint 配置和 vscode 并反应原生吗?
  • 请问.eslintrc.js的内容如何?
  • 阅读 README 以了解如何使用它,如果您只是在寻找配置的内容,请查看 index.js 文件
【解决方案2】:

我相信之前的答案对在这种情况下导致 eslint 抛出 react/prop-types 错误的原因存在误解。 lint 错误是正确的 - 缺少的是为 tabBarIcon 引入的箭头函数的道具验证。由于该箭头函数返回一个 React 组件,因此 eslint 执行react/prop-types 规则是正确的。为了满足规则,您需要为该箭头函数提供道具类型(将箭头函数视为一个匿名组件,它以color 作为道具)。只需添加 {color: string} 作为该箭头函数的整个参数的类型定义,如下所示:

({color}: {color: string}) =>

在上下文中:

<Tab.Screen
        name="Contacts"
        component={ContactStackScreen}
        options={{
          tabBarLabel: 'Contacts',
          tabBarColor: COLORS.DEFAULT,
          tabBarIcon: ({color}: {color: string}) => (
            <MaterialCommunityIcons name="contacts" color={color} size={26} />
          ),
        }}

【讨论】:

    【解决方案3】:

    根据文档,

    tabBarIcon 是底部选项卡导航器中支持的选项。所以我们知道我们可以在我们的屏幕组件中使用 options 属性,但是在这种情况下选择将它放在 Tab.Navigator 的 screenOptions 属性中是为了方便集中图标配置。 这是使用IconTab.Screen 的示例

    import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
    import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
    
    const Tab = createBottomTabNavigator();
    
    function MyTabs() {
    return (
    <Tab.Navigator
      initialRouteName="Feed"
      tabBarOptions={{
        activeTintColor: '#e91e63',
      }}
    >
      <Tab.Screen
        name="Feed"
        component={Feed}
        options={{
          tabBarLabel: 'Home',
          tabBarIcon: ({ color, size }) => (
            <MaterialCommunityIcons name="home" color={color} size={size} />
          ),
        }}
      />
      <Tab.Screen
        name="Notifications"
        component={Notifications}
        options={{
          tabBarLabel: 'Updates',
          tabBarIcon: ({ color, size }) => (
            <MaterialCommunityIcons name="bell" color={color} size={size} />
          ),
        }}
      />
      <Tab.Screen
        name="Profile"
        component={Profile}
        options={{
          tabBarLabel: 'Profile',
          tabBarIcon: ({ color, size }) => (
            <MaterialCommunityIcons name="account" color={color} size={size} />
          ),
        }}
      />
    </Tab.Navigator>
    

    ); }

    【讨论】:

    • 你好,我尝试将配置放在 screenOptions 中,但我得到了同样的错误,是关于道具类型
    猜你喜欢
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 2022-01-20
    • 2020-11-22
    • 2021-04-02
    • 2022-10-07
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多