react - 生命周期

v16.3+

  • Mounting
    • constructor(props)
    • static getDerivedStateFromProps(props, state)
    • render()
    • componentDidMount()
  • Updating
    • static getDerivedStateFromProps()
    • shouldComponentUpdate(nextProps, nextState)
    • render()
    • getSnapshotBeforeUpdate(prevProps, prevState)
    • componentDidUpdate(prevProps, prevState, snapshot)

constructor(props)

React 组件的构造函数在安装之前被调用。在为 React.Component 子类实现构造函数时,应该在任何其他语句之前调用 super(props)
否则,this.props 将在构造函数中未定义,这可能导致错误。

Avoid copying props into state! This is a common mistake:

1
2
3
4
5
constructor(props) {
super(props)
// Don't do this!
this.state = { color: props.color }
}

static getDerivedStateFromProps(nextProps, prevState)

props / state 改变时触发,需要返回一个对象或者 null,相当于 setState

  • demo
1
2
3
4
static getDerivedStateFromProps(nextProps, prevState){
if (nextProps.sum !== prevState.sum) return { sum: nextProps.sum } // 类似于 setState({ sum: nextProps.sum })
return null
}

render()

1
2
3
4
5
render(){
// don't do this
this.setState({ num: 12 })
return null
}

componentDidMount()

组件挂载后。

shouldComponentUpdate(nextProps, nextState)

1
shouldComponentUpdate(nextProps, nextState)

return true / false 来决定是否重新 render

getSnapshotBeforeUpdate(prevProps, prevState)

相当于 componentWillUpdate

componentDidUpdate(prevProps, prevState, snapshot)

更新后 - 这里谨慎使用 setState()

v16.3 以下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import React, { Component } from 'react'

/**
*
* 挂载数据:
* @example constructor => componentWillMount => render => componentDidMount
*
* 数据变化:
* @example props change: componentWillReceiveProps => shouldComponentUpdate => componentWillUpdate => render => componentDidUpdate
* @example state change: shouldComponentUpdate => componentWillUpdate => componentDidUpdate
*
*/
class LifeCycle extends React.Component {
constructor() {
super() // 声明constructor时必须调用super方法
this.state = {
subNum: 2
}
console.log('01 constructor')
}

componentWillMount() {
console.log('02 componentWillMount')
}

componentDidMount() {
console.log('04 componentDidMount')
}

componentWillReceiveProps(nextProps) {
console.log('05 componentWillReceiveProps')
}

shouldComponentUpdate(nextProps, nextState) {
console.log('06 shouldComponentUpdate')
return true // 记得要返回true
}

componentWillUpdate(nextProps, nextState) {
console.log('07 componentWillUpdate')
}

componentDidUpdate(prevProps, prevState) {
console.log('08 componentDidUpdate')
}

componentWillUnmount() {
console.log('09 componentWillUnmount')
}

changeState = () => {
this.setState(prevState => ({
subNum: ++prevState.subNum
}))
}

render() {
return (
<div>
<button onClick={this.changeState}>change state</button>
<h2>{this.state.subNum}</h2>
</div>
)
}
}

class App extends Component {
state = {
num: 1
}

changeProps = () => {
// this.setState((prevState, props) => ({}))
this.setState(prevState => ({
num: ++prevState.num
}))
}

render() {
return (
<div>
<button onClick={this.changeProps}>change props</button>
<hr />
<LifeCycle num={this.state.num} />
</div>
)
}
}

export default App