React生命周期以及關於17.0版本生命周期的改變


React的生命周期:

constuctor:

1組件的初始化,用來定義當前組件所需要的一些狀態,狀態定義在this.state

2、當前生命周期中必須書寫super,否則this的指向會發生錯誤以及報錯

3、在當前生命周期中默認是訪問不到props屬性的,如果想要進行訪問必須在super以及constructor中添加參數props

componentWillMount:

  掛載前:

        1可以進行前后端數據的請求(在服務端渲染的時候)

        2、可以在數據第一次被渲染的時候做數據的更改

        3、在當前生命周期中盡量不要調用this.setState因為當前生命周期函數執行完畢后,會自動執行render函數

        4、可以將外部的數據轉換為內部的數據

 

        注意:當前生命周期在17.0中已經廢除掉了

render:

1當前生命周期用來進行數據與模板的結合

    2render函數第一次執行的時候會將渲染的數據在內存中保存一份,當第二次數據發生了改變后,render會將這次的虛擬DOM與緩存中的虛擬DOM進行對比 這種對比叫做DIFF算法

    3只要this.state/this.props發生了改變那么render函數就會執行

componentDidMount:

掛載后:

        1、當前生命周期我們可以做前后端數據的交互

        2、可以在當前生命周期中獲取到真實的DOM  通過this.refs來獲取

        3一般情況下我們都在當前生命周期中做一些插件的實例化

            new Swiper('')

     操作真實DOM的方式

         ref="h2"    this.refs.h2

         ref={(el)=>{this.dom = el}}  this.dom

componentwillReceiveProps(newProps):

  1、當this.props發生改變的時候當前函數就會執行

    2、當前函數中會有一個參數 這個參數是一個新的props

    3在當前生命周期函數中我們可以對新的props做修改

    4、當前生命周期函數在17.0中廢除掉了

shouldComponentUpdate(newProps,newState):

  1、當this.state/this.props被修改的時候會執行當前生命周期函數

    2、當前生命周期執行的時候必須返回一個true或者是false 返回值決定了render函數是否會執行,如果為truerender函數執行falserender函數不會執行

    3、如果返回值為true則下面的生命周期會執行,如果為false則下面的生命周期不會執行

    4當前生命周期特別重要,因為當前生命可以做React的性能優化,(根據比較新舊的state/props來進行對)

    5、當前生命周期函數中有2個參數一個是新的props  一個是新的state

    6當期生命周期決定的是render函數是否執行,而不是數據是否修改

componentWillUpdate(newProps,newState):

 更新前:

        1在當前生命周期中我們可以對更新的數據做最后的修改

        2、當前生命周期中有2個參數 一個是新的props一個是新的state

注意:當前生命周期在17.0中已經廢除掉了

componentDidUpdate:

更新后

      1當前生命周期中我們可以獲取到數據更新后最新的DOM結構

      2、注意當前生命周期會執行多次,所以當你需要做業務邏輯操作的時候一定要判斷

componentWillUnmount:

卸載

    1當前生命周期執行的時候我們需要做事件的解綁

    2、數據的移除等操作

 

 

總結:

在第一次渲染時執行的周期函數有:

Constructor;componentWillMount;render;componentDidMount

this.props/this.state發生改變的時候執行生命周期

this.props

        componentWillReceiveProps

        shouldComponentUpdate

        componentWillUpdate

        render

        componentDidUpdate

    this.state

        shouldComponentUpdate

        componentWillUpdate

        render

        componentDidUpdate

React中哪些生命周期會執行一次,哪些生命周期會執行多次

        多次

       componentWillReceiveProps;shouldComponentUpdate;componentWillUpdate

       render;componentDidUpdate

        一次

        constructor;componentWillMount;componentDidMount

        componentWillUnmount

 

17版本中廢除的生命周期有(componentWillMountcomponentwillReceivePropscomponentWillUpdate),與之增加的生命周期有:

getDerivedStateFromProps(nextProps, prevState)

1、根據傳入的 props 來更新 state

2、方法是一個 static 方法意味着這個方法是屬於 React.Component 類的方法,所以方法內是無法使用 this 的,這就意味着無法使用 this.setState 來更新 state,所以這個方法直接通過返回對象的形式來更新 state,如果某些 props 的情況不需要更新 state,那么就返回 null 就好。實際上這個方法和 componentDidUpdate 搭配使用,就能覆蓋 componentWillReceiveProps 的所有使用場景了

 

 

getSnapshotBeforeUpdate

1、在更新之前獲取組件的快照在組件更新前觸發

2、它的返回值會作為第三個參數傳遞給后面的 componentDidUpdate 參數中,和 componentDidUpdate 一起使用,能覆蓋掉 componentWillUpdate 的所有使用場景了

 

 

componendDidCatch(error, info)

  如果一個組件定義了componentDidCatch生命周期,則他將成為一個錯誤邊界(錯誤邊界會捕捉渲染期間、在生命周期方法中和在它們之下整棵樹的構造函數中的錯誤,

  就像使用了try catch,不會將錯誤直接拋出了,保證應用的可用性)

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM