Constructor vs GetInitialState

Constructor and getInitialState both looks similar but you cannot use it interchangeably. These are two different approaches. When using ES6 classes you should initialize state in the constructor. If you are using React.createClass define the getInitialState method. Use getInitialState as given below:

var MyComponent =React.createClass({

getInitialState() {

return { /*initial state */ };



In the constructor assign to this.state directly because you cannot assign anywhere else. Use this.setState everywhere else.