the difference between using constructor vs getInitialState in React

today when i want to write a demo to practice React,I met a problem about getInitialState and constructor,now I understand the difference lie in we use ES6 or ES5 grammer,the following example explain it,so we should notice about ES6 grammer in React, it very easy made me make a mistake

  • ES5
var TodoApp = React.createClass({ 
  getInitialState () { 
    return {
      items: []
    }; 
  }
});
  • ES6
class TodoApp extends React.Component {
  constructor () {
    super()
    this.state = {
      items: []
    }
  }
});

你可能感兴趣的:(the difference between using constructor vs getInitialState in React)