允许`let state = {`的eslint规则

时间:2017-10-26 11:27:57

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

这是我的eslintrc。它抱怨 let state = {dataObjects: insurances }不正确。 我该怎么做才能解决这个问题?否则代码运行没有错误。

.eslintrc

{
   "extends": "airbnb",
   "env": {
       "es6": true
   },
   "parserOptions": {
    "sourceType": "module"
   }
}

TextablesScreen

   12 class TextablesScreen extends React.PureComponent {
   13   /* ***********************************************************
   14   * STEP 1
   15   * This is an array of objects with the properties you desire
   16   * Usually this should come from Redux mapStateToProps
   17   ************************************************************ */
>> 18   let state = {
   19     dataObjects: insurances
   20   }

1 个答案:

答案 0 :(得分:3)

在React中,this.state应被视为不可变。使用let表示状态毫无意义,而应将其添加为类属性

class TextablesScreen extends React.PureComponent {
  state = {
    dataObjects: insurances
  }
  /* ... */

如果需要props填充默认状态,请在构造函数中设置状态:

constructor(props) {
  super(props)
  this.state = ...
}