React Without ES6: Setting the Initial State

Setting the Initial State

//with ES6

class Counter extends React.Component {
  constructor(props) {
    super(props);
    this.state = {count: props.initialCount};
  }
  // ...
}


//without ES6

var Counter = createReactClass({
  getInitialState: function() {
    return {count: this.props.initialCount};
  },
  // ...
});

In ES6 classes, you can define the initial state by assigning this.state in the constructor. With createReactClass(), you have to provide a separate getInitialState method that returns the initial state.

Related concepts

Setting the Initial State

React Without ES6: Setting the Initial State — Structure map

Clickable & Draggable!

React Without ES6: Setting the Initial State — Related pages: