1.旧版本

# 1.挂载
# 2.更新 setState forupdate
class App extends React.Component {
constructor(props) {
super(props)
console.log('constructor')
this.state = {
count: 0
}
}
componentWillMount() {
console.log('componentWillMount')
}
componentDidMount() {
console.log('componentDidMount')
}
shouldComponentUpdate() {
console.log('shouldComponentUpdate')
return true
}
componentWillUpdate() {
console.log('componentWillUpdate')
}
componentDidUpdate() {
console.log('componentDidUpdate')
}
componentWillUnmount() {
console.log('componentWillUnmount')
}
render() {
console.log('render')
return (
<div>
React生命周期--{this.state.count}
<button onClick={() => this.change()}>click</button>
<button onClick={() => this.remove()}>remove</button>
<button onClick={() => this.force()}>force</button>
</div>
)
}
change() {
let count = this.state.count
this.setState({ count: count + 1})
}
remove() {
ReactDOM.unmountComponentAtNode(document.getElementById('root'))
}
force() {
this.forceUpdate()
}
}
# 父组件更新触发子组件
class Child extends React.Component {
componentWillReceiveProps() {
console.log('Child, componentWillReceiveProps')
}
shouldComponentUpdate() {
console.log('shouldComponentUpdate')
return true
}
componentWillUpdate() {
console.log('componentWillUpdate')
}
componentDidUpdate() {
console.log('componentDidUpdate')
}
render() {
console.log('render')
return (
<div>child {this.props.count}</div>
)
}
}
class App extends React.Component {
state = {
count: 0
}
change() {
let count = this.state.count
this.setState({ count: count + 1 })
}
render() {
return (
<div>
<Child />
<div>father</div>
<div>{this.state.count}</div>
<button onClick={() => this.change()}>change</button>
</div>
)
}
}
2.更名的声明周期
# 1.挂载
# 2.更新 setState forupdate
class App extends React.Component {
constructor(props) {
console.log('constructor')
super(props)
this.state = {
count: 0
}
}
UNSAFE_componentWillMount() {
console.log('UNSAFE_componentWillMount')
}
componentDidMount() {
console.log('componentDidMount')
}
componentWillUnmount() {
console.log('componentWillUnmount')
}
shouldComponentUpdate() {
console.log('shouldComponentUpdate')
return false
}
UNSAFE_componentWillUpdate() {
console.log('UNSAFE_componentWillUpdate')
}
componentDidUpdate() {
console.log('componentDidUpdate')
}
render () {
console.log('render')
return (
<div>
React声明周期
<div>{this.state.count}</div>
<button onClick={() => this.change()}>change</button>
<button onClick={() => this.remove()}>remove</button>
<button onClick={() => this.force()}>force</button>
</div>
)
}
change() {
const count = this.state.count
this.setState({ count: count + 1 })
}
remove() {
ReactDOM.unmountComponentAtNode(document.getElementById('root'))
}
force() {
this.forceUpdate()
}
}
# 父组件更新触发子组件
class Child extends React.Component {
UNSAFE_componentWillReceiveProps() {
console.log('UNSAFE_componentWillReceiveProps')
}
render() {
return (
<div>
<div>Child</div>
<div>{this.props.count}</div>
</div>
)
}
}
class App extends React.Component {
state = {
count: 0
}
render() {
return (
<div>
<Child count={this.state.count} />
<div>Father</div>
<div>{this.state.count}</div>
<button onClick={() => this.change()}>change</button>
</div>
)
}
change() {
const count = this.state.count
this.setState({ count: count + 1 })
}
}
2.新版本

# 新加入的生命周期:getDerivedStateFromProps(极少使用)
<App count={200} />
class App extends React.Component {
constructor(props) {
console.log('constructor')
super(props)
this.state = {
count: 0
}
}
componentDidMount() {
console.log('componentDidMount')
}
static getDerivedStateFromProps(props) {
console.log('getDerivedStateFormProps', props)
return {
count: props.count
}
}
render () {
console.log('render')
return (
<div>
React声明周期
<div>{this.state.count}</div>
<button onClick={() => this.change()}>change</button>
<button onClick={() => this.remove()}>remove</button>
<button onClick={() => this.force()}>force</button>
</div>
)
}
change() {
const count = this.state.count
this.setState({ count: count + 1 })
}
remove() {
ReactDOM.unmountComponentAtNode(document.getElementById('root'))
}
force() {
this.forceUpdate()
}
}
# 新加入的生命周期:getSnapshotBeforeUpdate(极少使用)
class App extends React.Component {
constructor(props) {
console.log('constructor')
super(props)
this.state = {
count: 0
}
}
getSnapshotBeforeUpdate() {
console.log('getSnapshotBeforeUpdate')
return 'getSnapshotBeforeUpdate'
}
componentDidUpdate(prevProps, prevState, snapshot) {
console.log('componentDidUpdate', prevProps, prevState, snapshot)
}
render () {
console.log('render')
return (
<div>
React声明周期
<div>{this.state.count}</div>
<button onClick={() => this.change()}>change</button>
</div>
)
}
change() {
const count = this.state.count
this.setState({ count: count + 1 })
}
}