【问题标题】:I want to change my code to match the airbnb rules我想更改我的代码以匹配 airbnb 规则
【发布时间】:2019-09-24 16:55:57
【问题描述】:
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import PropType from 'prop-types';
import Counter from '../components/Counter';
import * as counterActions from '../store/modules/counter';

class CounterContainer extends Component {
  handleIncrement = () => {
    console.log('+');
    const { CounterActions } = this.props;
    CounterActions.increment();
  }

  handleDecrement = () => {
    console.log('-');
    const { CounterActions } = this.props;
    CounterActions.decrement();
  }

  render() {
    const { handleIncrement, handleDecrement } = this;
    const { number } = this.props;

    return (
      <Counter
        onIncrement={handleIncrement}
        onDecrement={handleDecrement}
        number={number}
      />
    );
  }
}

我正在使用 ESLint airbnb。 当我写上面的代码 ESLint

引发错误,道具验证中缺少“CounterActions”。并且道具验证中缺少“数字”

所以我加了

CounterContainer.propTypes = {
  number: PropType.number,
  CounterActions: PropType.func,
};

propType "number" 不是必需的,但没有对应的 defaultProps 声明。 propType "CounterActions" 不是必需的,但没有相应的 defaultProps 声明。

从现在开始我不知道如何更改它。 我正在尝试在遵循本教程的同时应用 ESLint。 怎么改?

【问题讨论】:

    标签: reactjs react-native react-redux


    【解决方案1】:

    它要求您设置defaultProps,因为它们都是可选的。由于看起来它们都是此组件所必需的,因此请将它们设置为 isRequired

    CounterContainer.propTypes = {
      number: PropType.number.isRequired,
      CounterActions: PropType.func.isRequired,
    };
    

    这将绕过这些道具的react/require-default-props 规则。

    【讨论】:

      猜你喜欢
      • 2016-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多