文章目錄
- 和舊的生命周期相比
- React16 之后有三個生命周期被廢棄(但并沒有洗掉)
- 新版本的生命周期新增的鉤子
- getDerivedStateFromProps
- getSnapshotBeforeUpdate
- 總結
- 生命周期的三個階段(新)
- 重要的勾子
- 即將廢棄的勾子
和舊的生命周期相比

準備廢棄三個鉤子,已經新增了兩個鉤子
React16 之后有三個生命周期被廢棄(但并沒有洗掉)
- componentWillMount( 組件將要掛載的鉤子)
- componentWillReceiveProps(組件將要接收一個新的引數時的鉤子)
- componentWillUpdate(組件將要更新的鉤子)
新版本的生命周期新增的鉤子
- getDerivedStateFromProps
- 通過引數可以獲取新的屬性和狀態
- 該函式是靜態的
- 該函式的回傳值會覆寫掉組件狀態
- getSnapshotBeforeUpdate
- 真實的DOM構建完成,但還未實際渲染到頁面中,
- 在該函式中,通常用于實作一些附加的dom操作
- 該函式的回傳值,會作為componentDidUpdate的第三個引數
getDerivedStateFromProps
getDerivedStateFromProps不是給實體用的,需要將它定義為一個靜態方法,且需要給一個回傳值
回傳值可以使 state Obj 也可以是null
回傳值是 state Obj 的話直接將之前的覆寫 且無法改變
回傳null 對其他任何功能都沒有影響
// 從props哪里得到一個派生的狀態
static getDerivedStateFromProps(props,state){
return props
}
若 state的值 在人和時候都取決與 props 時,可以使用getDerivedStateFromProps

<div id="test"></div>
<!-- 引入react核心庫 -->
<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 轉換為 js -->
<script src="../js/17.0.1/babel.min.js"></script>
<script type='text/babel'>
// 創建組件
class Count extends React.Component{
// 構造器
constructor(props){
console.log('Count---constructor')
super(props)
// 初始化狀態
this.state = {count:0}
}
// 掛載完成的鉤子
componentDidMount(){
console.log('Count---componentDidMount')
}
// 卸載組件按鈕的回呼
death=()=>{
ReactDOM.unmountComponentAtNode(document.getElementById('test'))
}
// 實作 +1
add =()=>{
// 獲取原狀態
const {count} = this.state
// 更新狀態
this.setState({count:count+1})
}
// 強制更新按鈕的回呼
force=()=>{
this.forceUpdate()
}
static getDerivedStateFromProps(props,state){
console.log('getDerivedStateFromProps',props,state)
return props
}
// 控制組件更新的閥門
shouldComponentUpdate(){
console.log('Count---shouldComponentUpdate')
// 如果回傳值為false閥門關閉 默認為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>當前求和為:{count}</h2>
<button onClick={this.add}>點我+1</button>
<button onClick={this.death}>點我卸載組件</button>
<button onClick={this.force}>點我強制更新(不改變資料)</button>
</div>
)
}
}
// 渲染組件
ReactDOM.render(<Count count={166}/>,document.getElementById('test'))
</script>
執行結果

getSnapshotBeforeUpdate
回傳值可以是null 或者 一個快照
如果是null 則沒有任何影響
如果是一個快照則可以將回傳值傳遞給componentDidUpdate 的第三個引數
componentDidUpdate 能接收的三個引數
分別是
先前的props、先前的state和getSnapshotBeforeUpdate回傳的快照
prevprops、 prevstate、snapshotValue

<div id="test"></div>
<!-- 引入react核心庫 -->
<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 轉換為 js -->
<script src="../js/17.0.1/babel.min.js"></script>
<script type='text/babel'>
// 創建組件
class Count extends React.Component{
// 構造器
constructor(props){
console.log('Count---constructor')
super(props)
// 初始化狀態
this.state = {count:0}
}
// 掛載完成的鉤子
componentDidMount(){
console.log('Count---componentDidMount')
}
// 卸載組件按鈕的回呼
death=()=>{
ReactDOM.unmountComponentAtNode(document.getElementById('test'))
}
// 實作 +1
add =()=>{
// 獲取原狀態
const {count} = this.state
// 更新狀態
this.setState({count:count+1})
}
// 強制更新按鈕的回呼
force=()=>{
this.forceUpdate()
}
static getDerivedStateFromProps(props,state){
console.log('getDerivedStateFromProps',props,state)
return null
}
getSnapshotBeforeUpdate(){
console.log('getSnapshotBeforeUpdate');
return "eee"
}
// 控制組件更新的閥門
shouldComponentUpdate(){
console.log('Count---shouldComponentUpdate')
// 如果回傳值為false閥門關閉 默認為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>當前求和為:{count}</h2>
<button onClick={this.add}>點我+1</button>
<button onClick={this.death}>點我卸載組件</button>
<button onClick={this.force}>點我強制更新(不改變資料)</button>
</div>
)
}
}
// 渲染組件
ReactDOM.render(<Count count={166}/>,document.getElementById('test'))
</script>
總結
生命周期的三個階段(新)
一、 初始化階段: 由ReactDOM.render()觸發—初次渲染
- constructor()
- getDerivedStateFromProps
- render()
- componentDidMount()
二、 更新階段: 由組件內部this.setSate()或父組件重新render觸發
- getDerivedStateFromProps
- shouldComponentUpdate()
- render()
- getSnapshotBeforeUpdate
- componentDidUpdate()
三、卸載組件: 由ReactDOM.unmountComponentAtNode()觸發
- componentWillUnmount()
重要的勾子
- render:初始化渲染或更新渲染呼叫
- componentDidMount:開啟監聽, 發送ajax請求
- componentWillUnmount:做一些收尾作業, 如: 清理定時器
即將廢棄的勾子
- componentWillMount
- componentWillReceiveProps
- componentWillUpdate
現在使用會出現警告,下一個大版本需要加上UNSAFE_前綴才能使用,以后可能會被徹底廢棄,不建議使用,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/281277.html
標籤:其他
上一篇:【Vue】我想求助一下,關于vue洗掉組件總是洗掉最后一個組件,也就是最新添加的組件。
下一篇:【php基礎入門】PHP環境搭建與初識php代碼書寫及演示PHP和JS中遍歷陣列的區別、引入外部檔案等知識點學習筆記
