React的生命週期

constructor(props, context)

構造函數在創建組件的時候被調用;

void componentWillMount()

在掛載之前被調用,如果在這個函數裏面調用setState,本次的render函數可以看到更新後的state,並且只渲染一次。

void componentDidMount()

在掛載之後被調用

void componentWillReceiveProps(nextProps)

props是父組件傳遞給子組件的。父組件發生render的時候子組件就會調用componentWillReceiveProps(不管props有沒有更新,也不管父子組件之間有沒有數據交換)。

bool shouldComponentUpdate(nextProps, nextState)

組件掛載之後,每次調用setState後都會調用shouldComponentUpdate判斷是否需要重新渲染組件。

void componentWillUpdate(nextProps, nextState)


shouldComponentUpdate返回true或者調用forceUpdate之後,componentWillUpdate會被調用。

void componentDidUpdate()

除了首次render之後調用componentDidMount,其它render結束之後都是調用componentDidUpdate。

componentWillMount、componentDidMount和componentWillUpdate、componentDidUpdate可以對應起來。區別在於,前者只有在掛載的時候會被調用;而後者在以後的每次更新渲染之後都會被調用。

void componentWillUnmount()
組件被卸載的時候調用。一般在componentDidMount裏面註冊的事件需要在這裏刪除。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章