0
点赞
收藏
分享

微信扫一扫

React componentDidUpdate 和 componentWillReceiveProps 的区别


参数

触发时机

更新 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  

举报

相关推荐

0 条评论