ReactNative組件的生命周期

初始化階段

  • constructor(props)
  • 這是組件類的構(gòu)造函數(shù),通常在此初始化state數(shù)據(jù)模型。
constructor(props) {
  super(props);
  this.state = {
    //key : value
  };
}
  • componentWillMount
  • 表示組件將要加載到虛擬DOM,在render方法之前執(zhí)行,整個生命周期只執(zhí)行一次。
componentWillMount() {
}
  • componentDidMount
  • 表示組件已經(jīng)加載到虛擬DOM,
  • render方法之后執(zhí)行,整個生命周期只執(zhí)行一次。
  • 通常在該方法中完成異步網(wǎng)絡(luò)請求或者集成其他JavaScript
componentDidMount() {
}

運行階段

  • componentWillReceiveProps(nextProps)
  • 在組件接收到其父組件傳遞的props的時候執(zhí)行,參數(shù)為父組件傳遞的props。
  • 在組件的整個生命周期可以多次執(zhí)行。
  • 通常在此方法接收新的props值,
  • 重新設(shè)置state。
componentWillReceiveProps(nextProps) {
  this.setState({
    //key : value
  });
}
  • shouldComponentUpdate(nextProps, nextState)
  • componentWillReceiveProps(nextProps)執(zhí)行之后立刻執(zhí)行;
  • 或者在state更改之后立刻執(zhí)行。
  • 該方法包含兩個參數(shù),分別是props和state。
  • 該方法在組件的整個生命周期可以多次執(zhí)行。
  • 如果該方法返回false,
  • componentWillUpdate(nextProps, nextState)及其之后執(zhí)行的方法都不會執(zhí)行,組件則不會進(jìn)行重新渲染
  • 用于攔截propsstate做一些邏輯判斷
shouldComponentUpdate(nextProps, nextState) {
  return true;
}
  • componentWillUpdate(nextProps, nextState)
  • shouldComponentUpdate(nextProps, nextState)函數(shù)執(zhí)行完畢之后立刻調(diào)用,
  • 該方法包含兩個參數(shù),分別是propsstate。
  • render()函數(shù)執(zhí)行之前調(diào)用。
  • 該方法在組件的整個生命周期可以多次執(zhí)行
componentWillUpdate(nextProps, nextState) {
}
  • componentDidUpdate(preProps, preState)
  • render()方法執(zhí)行之后立刻調(diào)用。
  • 該方法包含兩個參數(shù),分別是propsstate。
  • 該方法在組件的整個生命周期可以多次執(zhí)行。
  • 通常用于更新完畢在這里做一些操作
componentDidUpdate(preProps, preState) {
}
  • render()
  • render方法用于渲染組件。在初始化階段和運行期階段都會執(zhí)行。
render() {
  return(
    <View/>
  );
}

銷毀階段

  • componentWillUnmount()
  • 銷毀時調(diào)用,通常用來取消時間綁定
  • 移除虛擬DOM中對應(yīng)組建的數(shù)據(jù)結(jié)構(gòu)
componentWillUnmount() {
}
最后編輯于
?著作權(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)容