React Native 中 component 生命周期

React Native中的component跟Android中的activity,fragment等一樣,存在生命周期,下面先給出component的生命周期圖

3-3-component-lifecycle.jpg

getDefaultProps

object getDefaultProps()

執(zhí)行過一次后,被創(chuàng)建的類會有緩存,映射的值會存在this.props,前提是這個prop不是父組件指定的
這個方法在對象被創(chuàng)建之前執(zhí)行,因此不能在方法內(nèi)調(diào)用this.props ,另外,注意任何getDefaultProps()返回的對象在實例中共享,不是復(fù)制

getInitialState

object getInitialState()

控件加載之前執(zhí)行,返回值會被用于state的初始化值

componentWillMount

void componentWillMount()

執(zhí)行一次,在初始化render之前執(zhí)行,如果在這個方法內(nèi)調(diào)用setState,render()知道state發(fā)生變化,并且只執(zhí)行一次

render

ReactElement render()

render的時候會調(diào)用render()會被調(diào)用
調(diào)用render()方法時,首先檢查this.propsthis.state返回一個子元素,子元素可以是DOM組件或者其他自定義復(fù)合控件的虛擬實現(xiàn)
如果不想渲染可以返回null或者false,這種場景下,React渲染一個<noscript>標(biāo)簽,當(dāng)返回null或者false時,ReactDOM.findDOMNode(this)返回nullrender()方法是很純凈的,這就意味著不要在這個方法里初始化組件的state,每次執(zhí)行時返回相同的值,不會讀寫DOM或者與服務(wù)器交互,如果必須如服務(wù)器交互,在componentDidMount()方法中實現(xiàn)或者其他生命周期的方法中實現(xiàn),保持render()方法純凈使得服務(wù)器更準(zhǔn)確,組件更簡單

componentDidMount

void componentDidMount()

在初始化render之后只執(zhí)行一次,在這個方法內(nèi),可以訪問任何組件,componentDidMount()方法中的子組件在父組件之前執(zhí)行

從這個函數(shù)開始,就可以和 JS 其他框架交互了,例如設(shè)置計時 setTimeout 或者 setInterval,或者發(fā)起網(wǎng)絡(luò)請求

shouldComponentUpdate

boolean shouldComponentUpdate(
  object nextProps, object nextState
)

這個方法在初始化render時不會執(zhí)行,當(dāng)props或者state發(fā)生變化時執(zhí)行,并且是在render之前,當(dāng)新的props或者state不需要更新組件時,返回false

shouldComponentUpdate: function(nextProps, nextState) {
  return nextProps.id !== this.props.id;
}

當(dāng)shouldComponentUpdate方法返回false時,講不會執(zhí)行render()方法,componentWillUpdatecomponentDidUpdate方法也不會被調(diào)用

默認(rèn)情況下,shouldComponentUpdate方法返回true防止state快速變化時的問題,但是如果·state不變,props只讀,可以直接覆蓋shouldComponentUpdate用于比較propsstate的變化,決定UI是否更新,當(dāng)組件比較多時,使用這個方法能有效提高應(yīng)用性能

componentWillUpdate

void componentWillUpdate(
  object nextProps, object nextState
)

當(dāng)propsstate發(fā)生變化時執(zhí)行,并且在render方法之前執(zhí)行,當(dāng)然初始化render時不執(zhí)行該方法,需要特別注意的是,在這個函數(shù)里面,你就不能使用this.setState來修改狀態(tài)。這個函數(shù)調(diào)用之后,就會把nextPropsnextState分別設(shè)置到this.propsthis.state中。緊接著這個函數(shù),就會調(diào)用render()來更新界面了

componentDidUpdate

void componentDidUpdate(
  object prevProps, object prevState
)

組件更新結(jié)束之后執(zhí)行,在初始化render時不執(zhí)行

componentWillReceiveProps

void componentWillReceiveProps(
  object nextProps
)

當(dāng)props發(fā)生變化時執(zhí)行,初始化render時不執(zhí)行,在這個回調(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

void componentWillUnmount()

當(dāng)組件要被從界面上移除的時候,就會調(diào)用componentWillUnmount(),在這個函數(shù)中,可以做一些組件相關(guān)的清理工作,例如取消計時器、網(wǎng)絡(luò)請求等

總結(jié)

React Native的生命周期就介紹完了,其中最上面的虛線框和右下角的虛線框的方法一定會執(zhí)行,左下角的方法根據(jù)props state是否變化去執(zhí)行,其中建議只有在componentWillMount,componentDidMount,componentWillReceiveProps方法中可以修改state

英文地址:https://facebook.github.io/react/docs/component-specs.html#lifecycle-methods

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容