当我使用react时,我发现这两个生命周期太相似了,componentWillReceiveProps接收nextProps作为参数,shouldComponentUpdate接收nextProps和nextState作为参数,所以我认为shouldComponentUpdate可以做同样的事情以及更多,为什么react保持componentWillReceiveProps方法,我想知道是什么这两种方法之间的区别
import React from 'react'
import ReactDOM from 'react-dom'
class App extends React.Component{
constructor(props) {
super(props)
this.state = {
list: [{id: 1,val: 'aa'}, {id: 2, val: 'bb'}, {id: 3, val: 'cc'}]
}
}
click() {
this.state.list.reverse()
this.setState({})
}
render() {
return (
<ul>
<div onClick={this.click.bind(this)}>reverse</div>
{
this.state.list.map(function(item, index) {
return (
<Li key={item.id} val={item.val}></Li>
)
}.bind(this))
}
</ul>
)
}
}
class Li extends React.Component{
constructor(props) {
super(props)
}
componentDidMount() {
console.log('===did===')
}
componentWillUpdate(nextProps, nextState) {
console.log('===mount====')
}
render() {
return …Run Code Online (Sandbox Code Playgroud)