日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

深入了解React组件重新渲染的条件和生命周期

發布時間:2023/12/2 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 深入了解React组件重新渲染的条件和生命周期 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

React組件rerender的真正條件

  • 當前組件的State中的屬性改變時且當前組件的shouldcomponentupdate返回true,那么當前組件會rerender

  • 組件的props中的任一屬性的值有變化(即使這個任一屬性的值是對象,變化的僅僅是該對象中的某屬性的值,此刻也算props發生了變化)且當前組件的shouldcomponentupdate return true時且當期組件所有父級以上組件的shouldcomponentupdate return true,當前組件才會re-render

  • 當前組件的shouldcomponentupdate即使返回false,當前組件里子組件若滿足1中的條件,這個子組件依然會重新渲染

  • 各個生命周期的深層含義

    shouldComponentUpdate

    觸發時機1:

    when new props(當props中某屬性的值為對象時,該對象中某屬性值發生了變化,也會觸發該函數的調用) are being received且所有父級組件的shouldComponentUpdate得返回true當前組件才會觸發該回調

    觸發時機2:

    即使所有父級組件的shouldComponentUpdate返回false,當前組件的state有變化,當前組件的該回調依然會觸發

    默認值

    Defaults to true. This method is not called for the initial render or when forceUpdate() is used

    其他情況

    if shouldComponentUpdate() returns false, then componentWillUpdate(), render(), and componentDidUpdate() will not be invoked

    componentWillReceiveProps

    觸發時機1:

    當當前組件的props某屬性有變化時(包括這種情況:當props中某屬性的值為對象時,該對象中某屬性值發生了變化)且所有父級以上組件的shouldComponentUpdate返回true(當前組件的shouldComponentUpdate是否返回true不重要)時當前組件的該回調才會觸發調用

    觸發時機2:

    即使當前組件的shouldComponentUpdate return false且當前組件沒有props的更新,父級組件re-render了,當前組件該函數還是會觸發調用

    組件第一次渲染完成:mounting

    mouting含義

    whenever the Clock is rendered to the DOM for the first time. This is called "mounting" in React

    在組件內相應回調函數叫componentDidMount() componentWillMount

    組件被銷毀時:Unmounting

    whenever the DOM produced by the Clock is removed. This is called "unmounting" in React

    在組件內相應回調函數叫componentWillUnmount()

    組件正在被重新渲染: Updating

    在組件內相應回調函數叫componentWillUpdate componentDidUpdate


    更多專業前端知識,請上 【猿2048】www.mk2048.com

    總結

    以上是生活随笔為你收集整理的深入了解React组件重新渲染的条件和生命周期的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。