React React.createClass vs extends React.Component Set Initial State


Example

There are changes in how we are setting the initial states.

React.createClass

We have a getInitialState function, which simply returns an Object of initial states.

import React from 'react';

const MyComponent = React.createClass({
  getInitialState () {
    return {
      activePage: 1
    };
  },
  render() {
    return (
      <div></div>
    );
  }
});

export default MyComponent;

React.Component

In this version we declare all state as a simple initialisation property in the constructor, instead of using the getInitialState function. It feels less "React API" driven since this is just plain JavaScript.

import React from 'react';

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      activePage: 1
    };
  }
  render() {
    return (
      <div></div>
    );
  }
}

export default MyComponent;