面試官常問React的生命周期問題
React的生命周期
兩張圖帶你理解 React的生命周期
React的生命周期(舊)

class Life extends React.Component{
// 構(gòu)造器
constructor(props){
console.log('Life構(gòu)造器---constructor');
super(props)
this.state={num:0}
}
// 計算+1功能
add=()=>{
const {num} = this.state
this.setState({num:num+1})
}
// 刪除組件
death=()=>{
ReactDOM.unmountComponentAtNode(document.getElementById('text'))
}
force=()=>{
this.forceUpdate()
}
// 將要掛載
componentWillMount(){
console.log('Life將要掛載---componentWillMount');
}
// 已經(jīng)掛載
componentDidMount(){
console.log('Life已經(jīng)掛載---componentDidMount');
}
// 刪除觸發(fā)
componentWillUnmount(){
console.log('Life刪除觸發(fā)---componentWillUnmount');
}
// 是否應(yīng)該改變數(shù)據(jù)
shouldComponentUpdate(){
console.log('Life是否改變數(shù)據(jù)---shouldComponentUpdate');
return true
}
// 將要改變數(shù)據(jù)
componentWillUpdate(){
console.log('Life將要改變數(shù)據(jù)---componentWillUpdate');
}
// 改變數(shù)據(jù)
componentDidUpdate(){
console.log('Life改變數(shù)據(jù)---componentDidUpdate');
}
render(){
console.log('Life---render');
const {num} = this.state
return(
<div>
<h1>計數(shù)器:{num}</h1>
<button onClick={this.add}>點(diǎn)我+1</button>
<button onClick={this.death}>刪除</button>
<button onClick={this.force}>不更改任何狀態(tài)的數(shù)據(jù),強(qiáng)制更新</button>
</div>
)
}
}
// 渲染頁面
ReactDOM.render(<Life />, document.getElementById('text'))
掛載步驟

更新步驟

刪除

總結(jié): 初始化階段: 由ReactDOM.render()觸發(fā)—初次渲染
1. constructor() ---構(gòu)造器
2. componentWillMount() ---將要掛載
3. render() ---render
4. componentDidMount() ---掛載時更新階段: 由組件內(nèi)部this.setSate()或父組件重新render觸發(fā)
1. shouldComponentUpdate() ---是否要進(jìn)行更改數(shù)據(jù)
2. componentWillUpdate() ---將要更新數(shù)據(jù)
3. render()
4. componentDidUpdate() ---更新數(shù)據(jù)卸載組件: 由ReactDOM.unmountComponentAtNode()觸發(fā)
componentWillUnmount() ---卸載
React的生命周期(新)

生命周期的三個階段(新)
初始化階段: 由ReactDOM.render()觸發(fā)—初次渲染
1. constructor()
2. getDerivedStateFromProps
3. render()
4. componentDidMount()更新階段: 由組件內(nèi)部this.setSate()或父組件重新render觸發(fā)
1. getDerivedStateFromProps
2. shouldComponentUpdate()
3. render()
4. getSnapshotBeforeUpdate
5. componentDidUpdate()卸載組件: 由ReactDOM.unmountComponentAtNode()觸發(fā)
1. componentWillUnmount()
重要的勾子
1.render:初始化渲染或更新渲染調(diào)用
2.componentDidMount:開啟監(jiān)聽, 發(fā)送ajax請求
3.componentWillUnmount:做一些收尾工作, 如: 清理定時器
即將廢棄的勾子
1.componentWillMount
2.componentWillReceiveProps
3.componentWillUpdate
現(xiàn)在使用會出現(xiàn)警告,下一個大版本需要加上UNSAFE_前綴才能使用,以后可能會被徹底廢棄,不建議使用。
到此這篇關(guān)于面試官常問React的生命周期問題的文章就介紹到這了,更多相關(guān)React生命周期內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
react實(shí)現(xiàn)一個優(yōu)雅的圖片占位模塊組件詳解
這篇文章主要給大家介紹了關(guān)于react如何實(shí)現(xiàn)一個還算優(yōu)雅的占位模塊圖片組件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
React工作流程及Error Boundaries實(shí)現(xiàn)過程講解
這篇文章主要介紹了React工作流程及Error Boundaries實(shí)現(xiàn)過程講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-02-02
react中路由跳轉(zhuǎn)及傳參的實(shí)現(xiàn)
本文主要介紹了react中路由跳轉(zhuǎn)及傳參的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
詳細(xì)談?wù)凴eact中setState是一個宏任務(wù)還是微任務(wù)
學(xué)過react的人都知道,setState在react里是一個很重要的方法,使用它可以更新我們數(shù)據(jù)的狀態(tài),下面這篇文章主要給大家介紹了關(guān)于React中setState是一個宏任務(wù)還是微任務(wù)的相關(guān)資料,需要的朋友可以參考下2021-09-09
react如何同步獲取useState的最新狀態(tài)值
這篇文章主要介紹了react如何同步獲取useState的最新狀態(tài)值問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
React Hooks獲取數(shù)據(jù)實(shí)現(xiàn)方法介紹
這篇文章主要介紹了react hooks獲取數(shù)據(jù),文中給大家介紹了useState dispatch函數(shù)如何與其使用的Function Component進(jìn)行綁定,實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10
React實(shí)現(xiàn)監(jiān)聽粘貼事件并獲取粘貼板中的截圖
這篇文章主要介紹了React實(shí)現(xiàn)監(jiān)聽粘貼事件并獲取粘貼板中的截圖方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
詳解React Angular Vue三大前端技術(shù)
當(dāng)前世界中,技術(shù)發(fā)展非常迅速并且變化迅速,開發(fā)者需要更多的開發(fā)工具來解決不同的問題。本文就對于當(dāng)下主流的前端開發(fā)技術(shù)React、Vue、Angular這三個框架做個相對詳盡的探究,目的是為了解開這些前端技術(shù)的面紗,看看各自的廬山真面目。2021-05-05

