創(chuàng)建階段
getDefaultProps : 在創(chuàng)建類的時候調(diào)用,處理props的默認(rèn)值,getDefaultProps僅會被調(diào)用一次倦西,這里的意思是無論你會創(chuàng)建多少個ReactElement捉邢,這個函數(shù)都只執(zhí)行一次据过,之后的默認(rèn)props都會直接使用改函數(shù)的返回值劲适。
實例化階段
constructor(getInitialState):初始化組件state的值,返回值會賦值給this.state屬性,這是ES6版本的寫法,ES5中使用的是getInitialState.在ES6的版本中有些不太一樣悴侵,相對與getDefaultProps,ES6將默認(rèn)屬性對象作為了構(gòu)造函數(shù)的一個屬性筏餐,而getInitialState則變成了在其構(gòu)造器函數(shù)中給this.state賦值.
componentWillMount:render之前調(diào)用
render:渲染并返回一個虛擬dom
componentDidMount:render之后調(diào)用,在組件渲染完成后調(diào)用一次.
p.s:state屬性,主要用來存儲組件自身需要的數(shù)據(jù),每次數(shù)據(jù)的更新都是通過修改state屬性的值,rn內(nèi)部會監(jiān)聽state屬性,一旦變化即會觸發(fā)render方法更新dom
運行階段
- componentWillReceiveProps:用戶操作后或父組件更新后,根據(jù)用戶的操作行為對頁面進(jìn)行調(diào)整
- shouldComponentUpdate:攔截新的props或state
- componentWillUpdate:shouldComponentUpdate返回true的情況下執(zhí)行
- componentDidUpdate:組件更新后執(zhí)行
銷毀階段
- componentWillUnmount:銷毀時調(diào)用,通常做一些取消綁定
p.s:絕對不要在componentWillUpdate和componentDidUpdate中調(diào)用this.setState方法,否則將導(dǎo)致無限循環(huán)調(diào)用捂寿。