0

This is my eslintrc. It complains about let state = {dataObjects: insurances } not being correct. What should I do to fix this? The code is running without errors otherwise.

.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   }
martins
  • 9,669
  • 11
  • 57
  • 85
  • I think question title should be changed, as it misguides. What if someone really wants to allow let in his project :) – Ali Sajid Nov 09 '18 at 08:17

1 Answers1

3

In React, this.state should be treated as if it were immutable. Using let for states makes no sense, instead you should add it as a class property:

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

Or set state in constructor if you need props to populate default state:

constructor(props) {
  super(props)
  this.state = ...
}
David Hellsing
  • 106,495
  • 44
  • 176
  • 212
  • 2
    or set in `constuctor` using `this.state = ...` if you need `props` to populate default state. – Sulthan Oct 26 '17 at 11:46
  • If you want to update it later you should use `this.setState` [doc](https://facebook.github.io/react-vr/docs/components-props-and-state.html#state) – Gabriel Bleu Oct 26 '17 at 12:17
  • @Sulthan, thanks for the reply. Post it as an answer and I'll check it as correct. :-) – martins Oct 30 '17 at 08:22