简体   繁体   English

允许`let state = {`

[英]eslint rules that allows `let state = {`

This is my eslintrc. 这是我的eslintrc。 It complains about let state = {dataObjects: insurances } not being correct. 它抱怨let state = {dataObjects: insurances }不正确。 What should I do to fix this? 我应该怎么做才能解决这个问题? The code is running without errors otherwise. 该代码正在运行,没有错误。

.eslintrc .eslintrc

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

TextablesScreen Textables屏幕

   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   }

In React, this.state should be treated as if it were immutable. 在React中, this.state应该被视为不可变的。 Using let for states makes no sense, instead you should add it as a class property : 使用let表示状态没有任何意义,而应该将其添加为类属性

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

Or set state in constructor if you need props to populate default state: 或在构造器中设置状态(如果需要props以填充默认状态):

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

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

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