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: []
    }
  }
});
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,900評(píng)論 0 23
  • 軍營 直線加方塊的韻律 彈奏出一支牢不可摧之曲 列隊(duì) 無數(shù)點(diǎn)的迅集 構(gòu)成一塊有力的方陣 軍禮 威嚴(yán)的手勢(shì) 崇高的敬...
    南疆漁夫閱讀 391評(píng)論 3 4
  • 2017年8月2日如是家人溫玲,種種子第2天 發(fā)心:我今不是為了我個(gè)人而聞思修,而是為了六道輪回一切如母有情眾生,...
    溫馨霏玲閱讀 269評(píng)論 2 3
  • 那樹 依舊是屹立著 ...
    少了一顆流星閱讀 209評(píng)論 0 1
  • 細(xì)雨柔花折閑愁, 眼含春夏更入秋。 少年難解世間語, 只道殷勤添桔柚。 ...
    羊的閱讀 184評(píng)論 0 3