1

Version A:

class MainScreen extends Component {
  dateArray = [];

  datePreparation(){
    this.dateArray.push( moment(currentDate).format('YYYY-MM-DD') ) 
  }
}

Version B:

const dateArray = [];
class MainScreen extends Component {

  datePreparation(){
    dateArray.push( moment(currentDate).format('YYYY-MM-DD') ) 
  }
}

Came across this 2 different way of declaring variable, wondering if there is any significant differences. On version A, why we can't initialize with const/let/var for variable dateArray

SuicideSheep
  • 5,260
  • 19
  • 64
  • 117
  • Possible duplicate of [ES6 class variable alternatives](https://stackoverflow.com/questions/22528967/es6-class-variable-alternatives) – Pritish Vaidya Apr 12 '18 at 04:52

1 Answers1

0

Considering the dateArray should not be shared among multiple MainScreen instances, version A is better.

For version A, dateArray is a class member variable, don't need to declare with var/let/const. It's equal to

constructor () { this.dateArray = [] }

And btw, if you are not using something like mobx, version A/B will not trigger rerender if dateArray changed.

Stackia
  • 2,110
  • 17
  • 23