0 votes
in ReactJS by

What is the difference between constructor and getInitialState?

1 Answer

0 votes
by

You should initialize state in the constructor when using ES6 classes, and getInitialState() method when using React.createClass().

Using ES6 classes:

class MyComponent extends React.Component {
  constructor(props) {
    super(props)
    this.state = { /* initial state */ }
  }
}

Using React.createClass():

const MyComponent = React.createClass({
  getInitialState() {
    return { /* initial state */ }
  }
})

Related questions

0 votes
asked Jun 15, 2020 in C Plus Plus by Robindeniel
0 votes
asked Feb 23, 2021 in ReactJS by SakshiSharma
...