react新版本生命周期鉤子函數(shù)及用法詳解
和舊的生命周期相比
準(zhǔn)備廢棄三個(gè)鉤子,已經(jīng)新增了兩個(gè)鉤子
React16 之后有三個(gè)生命周期被廢棄(但并沒(méi)有刪除)
- componentWillMount( 組件將要掛載的鉤子)
- componentWillReceiveProps(組件將要接收一個(gè)新的參數(shù)時(shí)的鉤子)
- componentWillUpdate(組件將要更新的鉤子)
新版本的生命周期新增的鉤子
- getDerivedStateFromProps
- 通過(guò)參數(shù)可以獲取新的屬性和狀態(tài)
- 該函數(shù)是靜態(tài)的
- 該函數(shù)的返回值會(huì)覆蓋掉組件狀態(tài)
getSnapshotBeforeUpdate
- 真實(shí)的DOM構(gòu)建完成,但還未實(shí)際渲染到頁(yè)面中。
- 在該函數(shù)中,通常用于實(shí)現(xiàn)一些附加的dom操作
- 該函數(shù)的返回值,會(huì)作為componentDidUpdate的第三個(gè)參數(shù)
getDerivedStateFromProps
getDerivedStateFromProps不是給實(shí)例用的,需要將它定義為一個(gè)靜態(tài)方法。且需要給一個(gè)返回值
返回值可以使 state Obj 也可以是null
返回值是 state Obj 的話(huà)直接將之前的覆蓋 且無(wú)法改變
返回null 對(duì)其他任何功能都沒(méi)有影響
// 從props哪里得到一個(gè)派生的狀態(tài) static getDerivedStateFromProps(props,state){ return props }
若 state的值 在人和時(shí)候都取決與 props 時(shí),可以使用getDerivedStateFromProps
<div id="test"></div> <!-- 引入react核心庫(kù) --> <script src="../js/17.0.1/react.development.js"></script> <!-- 引入react-dom,用于支持react操作dom --> <script src="../js/17.0.1/react-dom.development.js"></script> <!-- 引入babel 用于將jsx 轉(zhuǎn)換為 js --> <script src="../js/17.0.1/babel.min.js"></script> <script type='text/babel'> // 創(chuàng)建組件 class Count extends React.Component{ // 構(gòu)造器 constructor(props){ console.log('Count---constructor') super(props) // 初始化狀態(tài) this.state = {count:0} } // 掛載完成的鉤子 componentDidMount(){ console.log('Count---componentDidMount') } // 卸載組件按鈕的回調(diào) death=()=>{ ReactDOM.unmountComponentAtNode(document.getElementById('test')) } // 實(shí)現(xiàn) +1 add =()=>{ // 獲取原狀態(tài) const {count} = this.state // 更新?tīng)顟B(tài) this.setState({count:count+1}) } // 強(qiáng)制更新按鈕的回調(diào) force=()=>{ this.forceUpdate() } static getDerivedStateFromProps(props,state){ console.log('getDerivedStateFromProps',props,state) return props } // 控制組件更新的閥門(mén) shouldComponentUpdate(){ console.log('Count---shouldComponentUpdate') // 如果返回值為false閥門(mén)關(guān)閉 默認(rèn)為true return true } // 組件更新完畢的鉤子 componentDidUpdate(){ console.log('Count---componentDidUpdate') } // 組件將要卸載的鉤子 componentWillUnmount(){ console.log('Count---componentWillUnmount'); } render(){ console.log('Count---render') const {count} = this.state return( <div> <h2>當(dāng)前求和為:{count}</h2> <button onClick={this.add}>點(diǎn)我+1</button> <button onClick={this.death}>點(diǎn)我卸載組件</button> <button onClick={this.force}>點(diǎn)我強(qiáng)制更新(不改變數(shù)據(jù))</button> </div> ) } } // 渲染組件 ReactDOM.render(<Count count={166}/>,document.getElementById('test')) </script>
執(zhí)行結(jié)果
getSnapshotBeforeUpdate
返回值可以是null 或者 一個(gè)快照
如果是null 則沒(méi)有任何影響
如果是一個(gè)快照則可以將返回值傳遞給componentDidUpdate 的第三個(gè)參數(shù)
componentDidUpdate 能接收的三個(gè)參數(shù)
分別是
先前的props、先前的state和getSnapshotBeforeUpdate返回的快照
prevprops、 prevstate、snapshotValue
<div id="test"></div> <!-- 引入react核心庫(kù) --> <script src="../js/17.0.1/react.development.js"></script> <!-- 引入react-dom,用于支持react操作dom --> <script src="../js/17.0.1/react-dom.development.js"></script> <!-- 引入babel 用于將jsx 轉(zhuǎn)換為 js --> <script src="../js/17.0.1/babel.min.js"></script> <script type='text/babel'> // 創(chuàng)建組件 class Count extends React.Component{ // 構(gòu)造器 constructor(props){ console.log('Count---constructor') super(props) // 初始化狀態(tài) this.state = {count:0} } // 掛載完成的鉤子 componentDidMount(){ console.log('Count---componentDidMount') } // 卸載組件按鈕的回調(diào) death=()=>{ ReactDOM.unmountComponentAtNode(document.getElementById('test')) } // 實(shí)現(xiàn) +1 add =()=>{ // 獲取原狀態(tài) const {count} = this.state // 更新?tīng)顟B(tài) this.setState({count:count+1}) } // 強(qiáng)制更新按鈕的回調(diào) force=()=>{ this.forceUpdate() } static getDerivedStateFromProps(props,state){ console.log('getDerivedStateFromProps',props,state) return null } getSnapshotBeforeUpdate(){ console.log('getSnapshotBeforeUpdate'); return "eee" } // 控制組件更新的閥門(mén) shouldComponentUpdate(){ console.log('Count---shouldComponentUpdate') // 如果返回值為false閥門(mén)關(guān)閉 默認(rèn)為true return true } // 組件更新完畢的鉤子 componentDidUpdate(preProps,preState,snapshotValue){ console.log('Count---1componentDidUpdate',preProps,preState,snapshotValue); } // 組件將要卸載的鉤子 componentWillUnmount(){ console.log('Count---componentWillUnmount'); } render(){ console.log('Count---render') const {count} = this.state return( <div> <h2>當(dāng)前求和為:{count}</h2> <button onClick={this.add}>點(diǎn)我+1</button> <button onClick={this.death}>點(diǎn)我卸載組件</button> <button onClick={this.force}>點(diǎn)我強(qiáng)制更新(不改變數(shù)據(jù))</button> </div> ) } } // 渲染組件 ReactDOM.render(<Count count={166}/>,document.getElementById('test')) </script>
總結(jié)
生命周期的三個(gè)階段(新)
一、 初始化階段: 由ReactDOM.render()觸發(fā)—初次渲染
constructor()getDerivedStateFromPropsrender()componentDidMount()
二、 更新階段: 由組件內(nèi)部this.setSate()或父組件重新render觸發(fā)
getDerivedStateFromPropsshouldComponentUpdate()render()getSnapshotBeforeUpdatecomponentDidUpdate()
三、卸載組件: 由ReactDOM.unmountComponentAtNode()觸發(fā)
componentWillUnmount()
重要的勾子
render:初始化渲染或更新渲染調(diào)用componentDidMount:開(kāi)啟監(jiān)聽(tīng), 發(fā)送ajax請(qǐng)求componentWillUnmount:做一些收尾工作, 如: 清理定時(shí)器
即將廢棄的勾子
- componentWillMount
- componentWillReceiveProps
- componentWillUpdate
現(xiàn)在使用會(huì)出現(xiàn)警告,下一個(gè)大版本需要加上UNSAFE_前綴才能使用,以后可能會(huì)被徹底廢棄,不建議使用。
到此這篇關(guān)于react新版本生命周期鉤子函數(shù)的文章就介紹到這了,更多相關(guān)react 生命周期 鉤子函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React-Native做一個(gè)文本輸入框組件的實(shí)現(xiàn)代碼
這篇文章主要介紹了React-Native做一個(gè)文本輸入框組件的實(shí)現(xiàn)代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-08-08使用Node搭建reactSSR服務(wù)端渲染架構(gòu)
這篇文章主要介紹了使用Node搭建reactSSR服務(wù)端渲染架構(gòu),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08react中form.setFieldvalue數(shù)據(jù)回填時(shí) value和text不對(duì)應(yīng)的問(wèn)題及解決方法
這篇文章主要介紹了react中form.setFieldvalue數(shù)據(jù)回填時(shí) value和text不對(duì)應(yīng)的問(wèn)題及解決方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07React-Hook中使用useEffect清除定時(shí)器的實(shí)現(xiàn)方法
這篇文章主要介紹了React-Hook中useEffect詳解(使用useEffect清除定時(shí)器),主要介紹了useEffect的功能以及使用方法,還有如何使用他清除定時(shí)器,需要的朋友可以參考下2022-11-11關(guān)于React項(xiàng)目中的PDF展示解決方案
這篇文章主要介紹了關(guān)于React項(xiàng)目中的PDF展示解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07詳解使用create-react-app添加css modules、sasss和antd
這篇文章主要介紹了詳解使用create-react-app添加css modules、sasss和antd,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07