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.
Semantic portal