简体   繁体   English

解决似乎没有意义的ESLint / React / Redux(Airbnb配置)错误

[英]Resolving ESLint / React / Redux (Airbnb config) errors that don't seem to make sense

I'm working through the discipline of using ESLint (Airbnb config) with React / Redux. 我正在研究使用ESLint(Airbnb配置)和React / Redux。

The following code is a standard type React / Redux class that I've written to enjoy dealing with all of Airbnb's linting discipline. 下面的代码是一个标准类型的React / Redux类,我写这篇文章是为了享受Airbnb的所有linting规则。

Most of the linting errors I've resolved by understanding what Airbnb's configuration prefers, however, there are a couple of areas that I presently don't understand. 通过了解Airbnb的配置偏好我已经解决了大部分掉毛错误,但是,我目前还有几个方面不了解。 These are: 这些是:

  1. The entire method renderTableHeader() is outlined in red and ESLint tells me: 整个方法renderTableHeader()以红色标出,ESLint告诉我:

[eslint] Expected 'this' to be used by class method 'renderTableHeader'. [eslint]期望'this'由类方法'renderTableHeader'使用。 (class-methods-use-this) (JSX attribute) className: string (class-methods-use-this)(JSX属性)className:string 在此输入图像描述

none of the other methods have this linting problem 没有其他方法有这个linting问题

  1. I connect an object from Redux state to props (contains many key/objects that I need to iterate over in this class). 我将一个从Redux状态的对象连接到props(包含我需要在这个类中迭代的许多键/对象)。 ESLint doesn't seem to like me wiring objects from state to props...giving me the message: ESLint似乎不喜欢我将对象从状态连接到道具......给我的消息:

[eslint] Prop type object is forbidden (react/forbid-prop-types) import PropTypes [eslint]禁止Prop类型object (react / forbid-prop-types)导入PropTypes

在此输入图像描述

My state object is an object containing many keys that reference user objects - so I need users to be an object. 我的状态对象是一个包含许多引用用户对象的键的对象 - 所以我需要users成为一个对象。 Is this bad practice? 这是不好的做法吗? If it is how would I connect an object to map over without this lint message appearing? 如果没有出现这个lint消息,我将如何连接一个要映射的对象?

Thanks in advance...here's my class: 在此先感谢...这是我的课程:

import React, { Component } from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import _ from 'lodash';
import { getUsers } from '../actions/getUsers';
import setUserMarketingPref from '../actions/setUserMarketingPref';
import { setFilter } from '../actions/setFilter';
import TableRow from '../components/TableRow';
import '../styles/styles.css';

class AppContainer extends Component {
  componentDidMount() {
    this.props.getUsers();
  }

  renderTableHeader() {
    return (
      <div className="table__header">
        <div className="table__header--name">Name</div>
        <div className="table__header--gender">Gender</div>
        <div className="table__header--region">Region</div>
      </div>
    );
  }

  renderTable() {
    const { users, filterMode } = this.props;
    const usersView = filterMode ? _.omitBy(users, user => !user.checked) : users;
    return _.map(usersView, user => (
      <TableRow
        user={user}
        key={user.id}
        setUserMarketingPref={_.debounce(() => this.props.setUserMarketingPref(user.id), 100)}
      />
    ));
  }

  renderFilters() {
    return (
      <div className="table__buttons">
        <button
          id="marketing-other"
          className="table__other"
          onClick={e => this.props.setFilter(e.target.id)}
        >Other filter
        </button>
        <button
          id="marketing-none"
          className="table__no-marketing"
          onClick={e => this.props.setFilter(e.target.id)}
        >No marketing
        </button>
      </div>
    );
  }

  render() {
    if (!_.size(this.props.users)) {
      return null;
    }
    return (
      <div className="table__container">
        {this.renderTableHeader()}
        {this.renderTable()}
        {this.renderFilters()}
      </div>
    );
  }
}

const mapDispatchToProps = dispatch => bindActionCreators(
  {
    getUsers,
    setUserMarketingPref,
    setFilter,
  },
  dispatch,
);

const mapStateToProps = state => ({
  users: state.users,
  filter: state.filter,
  filterMode: state.marketing.filterMode,
});

AppContainer.defaultProps = {
  filterMode: false,
  getUsers: null,
  setUserMarketingPref: null,
  setFilter: null,
};

AppContainer.propTypes = {
  users: PropTypes.object,
  filterMode: PropTypes.bool,
  getUsers: PropTypes.func,
  setUserMarketingPref: PropTypes.func,
  setFilter: PropTypes.func,
};

export default connect(mapStateToProps, mapDispatchToProps)(AppContainer);

The two rules address two different things 这两条规则涉及两个不同的事情

First: Expected 'this' to be used by class method 'renderTableHeader'. 第一:期望'this'由类方法'renderTableHeader'使用。 (class-methods-use-this) (JSX attribute) className: string (class-methods-use-this)(JSX属性)className:string

According to the documentation 根据文件

If a class method does not use this, it can sometimes be made into a static function. 如果类方法不使用它,有时可以将其作为静态函数。 If you do convert the method into a static function, instances of the class that call that particular method have to be converted to a static call as well (MyClass.callStaticMethod()) 如果您确实将方法转换为静态函数,则调用该特定方法的类的实例也必须转换为静态调用(MyClass.callStaticMethod())

Also note in the above examples that if you switch a method to a static method, instances of the class that call the static method (let a = new A(); a.sayHi();) have to be updated to being a static call (A.sayHi();) instead of having the instance of the class call the method 另请注意,在上面的示例中,如果将方法切换为静态方法,则调用静态方法的类的实例(let a = new A(); a.sayHi();)必须更新为静态方法call (A.sayHi();)而不是让类的实例调用该方法

How to avoid having this warning 如何避免此警告

The exceptMethods option allows you to pass an array of method names for which you would like to ignore warnings. exceptMethods选项允许您传递要忽略警告的方法名称数组。 For example, you might have a spec from an external library that requires you to overwrite a method as a regular function (and not as a static method) and does not use this inside the function body. 例如,您可能有一个来自外部库的规范要求您将方法覆盖为常规函数(而不是静态方法),并且不在函数体内使用它。 In this case, you can add that method to ignore in the warnings. 在这种情况下,您可以在警告中添加要忽略的方法。

"class-methods-use-this": [<enabled>, { "exceptMethods": [<...exceptions>] }]

Second: Prop type object is forbidden (react/forbid-prop-types) import PropTypes 第二:禁止Prop类型对象(react / forbid-prop-types)导入PropTypes

It is a good practise to define PropTypes that are not vague, example any , array , object which do not clearly tell you what kind the prop actually is. 最好定义不模糊的PropTypes,例如anyarrayobject ,它们不能清楚地告诉你prop的实际类型。

According to the documentation 根据文件

By default this rule prevents vague prop types with more specific alternatives available (any, array, object), but any prop type can be disabled if desired. 默认情况下,此规则可防止含有更多特定替代项的模糊prop类型(any,array,object),但如果需要,可以禁用任何prop类型。 The defaults are chosen because they have obvious replacements. 选择默认值是因为它们具有明显的替代品。 any should be replaced with, well, anything. 任何东西都应该被替换。 array and object can be replaced with arrayOf and shape, respectively. 数组和对象可以分别替换为arrayOf和shape。

In your case say is user is an object like 在你的情况下说user是一个像对象

{
    id: 123,
    name: 'abc'
}

you could define the PropType like 你可以像这样定义PropType

AppContainer.propTypes = {
  users: PropTypes.shape({
    id: PropTypes.number,
    name: PropTypes.string
  }),,
  filterMode: PropTypes.bool,
  getUsers: PropTypes.func,
  setUserMarketingPref: PropTypes.func,
  setFilter: PropTypes.func,
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM