日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

React生命周期

作者:qq_45689385 更新時間: 2022-07-21 編程語言

一、初始化階段(掛載):由ReactDOM.render()觸發 ---初次渲染

constructor:構造函數,在 React 組件掛載之前執行。

getDerivedStateFromProps:在調用 render 方法之前調用,并且在初始掛載及后續更新時都會被調用 需要返回一個null 或者 state 此返回的狀態是不可更改的,

render:渲染處理虛擬dom,是 class 組件中唯一必須實現的方法。

componentDidMount:在組件掛載后(插入 DOM 樹中)立即調用,此時就可以操作dom、發請求等

二、更新階段:由組件內部this.setSate()或父組件重新render觸發

getDerivedStateFromProps:與掛載一樣,掛載和更新都會執行。

shouldComponentUpdate:在render前調用執行,相當于閥門,可以返回一個布爾值,來判斷是否render

render:與掛載一樣,掛載和更新都會執行

getSnapshotBeforeUpdate:在最近一次渲染輸出(提交到 DOM 節點)之前調用。兩個參數 prevProps prevState 修改之前的值,返回值snapshot,例 return ‘張三’ 在componentDidUpdate中接收

componentDidUpdate:組件更新完畢的鉤子。接收三個參數 prevProps prevState snapshot

三、卸載組件: 由ReactDOM.unmountComponentAtNode()觸發

componentWillUnmount:在組件卸載及銷毀之前直接調用。一般可以用于清除定時器等

注:此文章正對于16+版本的

16+后沒有了? ?componentWillMount :組件將要掛載時的鉤子

? ? ? ? ? ? ? ? ? ? ? ?componentWillReceiveProps:組件將要接收新的props的鉤子

????????????????????????componentWillUpdate:組件將要更新的鉤子

最后推出個強制更新的方法?forceUpdate

16+圖解

?

????????????????????????

原文鏈接:https://blog.csdn.net/qq_45689385/article/details/124218631

欄目分類
最近更新