参数

触发时机

更新 state 的方式

componentDidUpdate

componentDidUpdate(preProps,preState,spanshot)

3个参数,上一次的props,上一次的state,快照

在组件接受新的props或新的state之后触发

props和state变化都会触发,所有在此更新状态一定要有判断条件

更新状态是异步的

触发重新render

componentWillReceiveProps

componentWillReceiveProps(nextProps,nextContext)

2个参数nextProps,下一次的props

在组件接受新的props之前触发

仅在props变化时会触发

更新状态是同步的

不触发重新render

componentWillReceiveProps即将废弃,推荐使用componentDidUpdate

React componentWillMount、componentDidMount、componentWillUpdate和componentDidUpdate生命周期函数的详解

React应用在初始化和更新的过程中,会经过一系列的逻辑,React在不同的逻辑时期会执行不同的生命周期函数,用来给我们做一些处理。 对于初次挂载来说,也就是整个React应用初始化时会执行componentWillMount和componentDidMount生命周期函数 对于更新应用时,比如父组 ...