https://reactjs.org/docs/react-component.html#lifecycle-methods
React Native中的component跟Android中的activity杆逗,fragment等一樣鳞疲,存在生命周期罪郊,下面先給出component的生命周期圖
getDefaultProps
object getDefaultProps()
執(zhí)行過一次后尚洽,被創(chuàng)建的類會有緩存,映射的值會存在this.props,前提是這個(gè)prop不是父組件指定的
這個(gè)方法在對象被創(chuàng)建之前執(zhí)行癣疟,因此不能在方法內(nèi)調(diào)用this.props ,另外睛挚,注意任何getDefaultProps()返回的對象在實(shí)例中共享,不是復(fù)制
getInitialState
object getInitialState()
控件加載之前執(zhí)行店溢,返回值會被用于state的初始化值
componentWillMount
void componentWillMount()
執(zhí)行一次委乌,在初始化render之前執(zhí)行荣回,如果在這個(gè)方法內(nèi)調(diào)用setState,render()知道state發(fā)生變化心软,并且只執(zhí)行一次
render
ReactElement render()
render的時(shí)候會調(diào)用render()會被調(diào)用
調(diào)用render()方法時(shí),首先檢查this.props和this.state返回一個(gè)子元素耳贬,子元素可以是DOM組件或者其他自定義復(fù)合控件的虛擬實(shí)現(xiàn) 如果不想渲染可以返回null或者false,這種場景下咒劲,React渲染一個(gè)<noscript>標(biāo)簽诫隅,當(dāng)返回null或者false時(shí),ReactDOM.findDOMNode(this)返回null
render()方法是很純凈的逐纬,這就意味著不要在這個(gè)方法里初始化組件的state,每次執(zhí)行時(shí)返回相同的值兔毒,不會讀寫DOM或者與服務(wù)器交互甸箱,如果必須如服務(wù)器交互,在componentDidMount()方法中實(shí)現(xiàn)或者其他生命周期的方法中實(shí)現(xiàn)摇肌,保持render()方法純凈使得服務(wù)器更準(zhǔn)確,組件更簡單
componentDidMount
void componentDidMount()
在初始化render之后只執(zhí)行一次昵骤,在這個(gè)方法內(nèi)树碱,可以訪問任何組件变秦,componentDidMount()方法中的子組件在父組件之前執(zhí)行
從這個(gè)函數(shù)開始,就可以和 JS 其他框架交互了赎婚,例如設(shè)置計(jì)時(shí) setTimeout 或者 setInterval樱溉,或者發(fā)起網(wǎng)絡(luò)請求
shouldComponentUpdate
boolean shouldComponentUpdate(
object nextProps, object nextState
)
這個(gè)方法在初始化render時(shí)不會執(zhí)行,當(dāng)props或者state發(fā)生變化時(shí)執(zhí)行福贞,并且是在render之前,當(dāng)新的props或者state不需要更新組件時(shí)完丽,返回false
shouldComponentUpdate: function(nextProps, nextState) {
return nextProps.id !== this.props.id;
}
當(dāng)shouldComponentUpdate方法返回false時(shí)逻族,講不會執(zhí)行render()方法骄崩,componentWillUpdate和componentDidUpdate方法也不會被調(diào)用
默認(rèn)情況下,shouldComponentUpdate方法返回true防止state快速變化時(shí)的問題刁赖,但是如果·state不變,props只讀鸡典,可以直接覆蓋shouldComponentUpdate用于比較props和state的變化枪芒,決定UI是否更新,當(dāng)組件比較多時(shí)舅踪,使用這個(gè)方法能有效提高應(yīng)用性能
componentWillUpdate
void componentWillUpdate(
object nextProps, object nextState
)
當(dāng)props和state發(fā)生變化時(shí)執(zhí)行,并且在render方法之前執(zhí)行抽碌,當(dāng)然初始化render時(shí)不執(zhí)行該方法,需要特別注意的是左权,在這個(gè)函數(shù)里面,你就不能使用this.setState來修改狀態(tài)赏迟。這個(gè)函數(shù)調(diào)用之后,就會把nextProps和nextState分別設(shè)置到this.props和this.state中甩栈。緊接著這個(gè)函數(shù)糕再,就會調(diào)用render()來更新界面了
componentDidUpdate
void componentDidUpdate(
object prevProps, object prevState
)
組件更新結(jié)束之后執(zhí)行,在初始化render時(shí)不執(zhí)行
componentWillReceiveProps
void componentWillReceiveProps(
object nextProps
)
當(dāng)props發(fā)生變化時(shí)執(zhí)行亿鲜,初始化render時(shí)不執(zhí)行冤吨,在這個(gè)回調(diào)函數(shù)里面,你可以根據(jù)屬性的變化垒探,通過調(diào)用this.setState()來更新你的組件狀態(tài)怠李,舊的屬性還是可以通過this.props來獲取,這里調(diào)用更新狀態(tài)是安全的,并不會觸發(fā)額外的render調(diào)用
componentWillReceiveProps: function(nextProps) {
this.setState({
likesIncreasing: nextProps.likeCount > this.props.likeCount
});
}
componentWillUnmount
當(dāng)組件要被從界面上移除的時(shí)候捺癞,就會調(diào)用componentWillUnmount(),在這個(gè)函數(shù)中,可以做一些組件相關(guān)的清理工作惕鼓,例如取消計(jì)時(shí)器、網(wǎng)絡(luò)請求等