【问题标题】:eslint error "data is missing in props validation" in apollo react app阿波罗反应应用程序中的eslint错误“道具验证中缺少数据”
【发布时间】:2017-03-29 17:10:46
【问题描述】:

不确定我是否理解这个 eslint 错误要求我做什么。我正在使用 apollo 客户端演示代码,它似乎不喜欢 function PostList({ data: { loading, posts } }) { 中的“数据”

我应该做其他事情来遵守airbnb eslint规则还是应该忽略它?

import React from 'react';
import { Text, View } from 'react-native';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';

const styles = {
  outer: { paddingTop: 22 },
  wrapper: { height: 45, flex: 1, flexDirection: 'row' },
  header: { fontSize: 20 },
  subtextWrapper: { flex: 1, flexDirection: 'row' },
  votes: { color: '#999' },
}

// The data prop, which is provided by the wrapper below contains,
// a `loading` key while the query is in flight and posts when ready
function PostList({ data: { loading, posts } }) {
  if (loading) {
    return <Text style={styles.outer}>Loading</Text>;
  } else {
    return (
      <View style={styles.outer}>
        {posts.sort((x, y) => y.votes - x.votes).map(post => (
          <View key={post.id} style={styles.wrapper}>
            <View>
              <Text style={styles.header}>{post.title}</Text>
              <View style={styles.subtextWrapper}>
              <Text>
                by {post.author.firstName} {' '}
                {post.author.lastName} {' '}
              </Text>
              <Text style={styles.votes}>{post.votes} votes</Text>
              </View>
            </View>
          </View>
        ))}
      </View>
    );
  }
}

// The `graphql` wrapper executes a GraphQL query and makes the results
// available on the `data` prop of the wrapped component (PostList here)
export default graphql(gql`
  query allPosts {
    posts {
      id
      title
      votes
      author {
        id
        firstName
        lastName
      }
    }
  }
`)(PostList);

【问题讨论】:

    标签: eslint react-apollo eslint-config-airbnb


    【解决方案1】:

    你必须在你的文件中添加 proptypes:

    PostList.propTypes = {
      data: React.PropTypes.object,
    };
    

    将此添加到您的 PostList 函数下方。 PropTypes 是一种验证传递给 React 应用程序组件的数据类型的方法。

    AirBnB linter 尽可能确保这是最佳实践。

    【讨论】:

    • 现在又一个 lint 错误 - 它说 prop 类型对象被禁止
    • 您可以使用React.PropTypes.any,它将通过验证。然而,我们的想法是,无论您传递什么,都是您声明为 PropType 的内容。
    • 另外,请查看link 了解有关禁止道具类型的更多信息 - 这将取决于您的特定 linter 配置。
    • 嗯,它也不喜欢任何 - 不知道是否足以决定 airbnb lint 样式(有一些更改)是否适合 apollo 客户端示例
    • 您的问题的答案是添加 PropTypes。但是,如果您的配置中的某些内容禁止某些 PropTypes,那么您将继续看到此错误。我使用 AirBnB linter,但我没有任何 PropTypes 被禁止。
    猜你喜欢
    • 2021-09-29
    • 2016-12-05
    • 2017-06-20
    • 1970-01-01
    • 2022-01-27
    • 2017-09-26
    • 2020-03-18
    • 2018-08-01
    • 2018-04-13
    相关资源
    最近更新 更多