componentWillReceiveProps,为何被废弃
componentWillReceiveProps的写法并不存在什么功能上的问题,将componentWillReceiveProps标记为 deprecated-弃用 的原因也并不是因为功能问题,而是性能问题。
componentWillReceiveProps是同步的,当外部多个属性在很短的时间间隔之内多次变化,就会导致componentWillReceiveProps被多次调用。 这个调用并不会被合并,如果这次内容都会触发异步请求,那么可能会导致多个异步请求阻塞。而setState操作是会通过transaction进行合并的, 由此导致的更新过程是批量的,而react中大部分的更新过程的触发源都是setState,所以render触发的频率并不会非常频繁。
因此在使用getDerivedStateFromProps的时候,遇到了上面说的props在很短的时间内多次变化,也只会触发一次render,也就是只触发一次getDerivedStateFromProps,从而提高了性能。
React16版本新增生命周期 getDerivedStateFromProps替代componentWillReceiveProps
1 含义
getDerivedStateFromProps生命周期的意思就是从props中获取state,其功能实际上就是将传入的props映射到state上面。 这个函数会在每次re-rendering之前被调用,意味着即使你的props没有任何变化,而是父state发生了变化,导致子组件发生了re-render,这个生命周期函数依然会被调用。
用一个静态函数getDerivedStateFromProps来取代被deprecate的几个生命周期函数,就是强制开发者在render之前只做无副作用的操作,而且能做的操作局限在根据props和state决定新的state,而已。
getDerivedStateFromProps exists for only one purpose. It enables a component to update its internal state as the result of changes in props.
从上边这句话中,我们可以清晰知道 getDerivedStateFromProps 的作用就是为了让 props 能更新到组件内部 state 中
2 用法比较
使用componentWillReceiveProps时,直接更新state。而getDerivedStateFromProps是一个静态函数,也就是这个函数不能通过this访问到class的属性,也并不推荐直接访问属性。 而是应该通过参数提供的nextProps以及prevState来进行判断,根据新传入的props来映射到state。 需要注意的是,如果props传入的内容不需要影响到你的state,那么就需要返回一个null,这个返回值是必须的,所以尽量将其写到函数的末尾。
this.getRevenueShareList();在这个方法中,请求接口数据,更新state。
3 React v16.3之后的生命周期函数一览图。
4 componentWillReceiveProps和componentDidUpdate区别。
14 Dec 2020