Open zhouzhongyuan opened 7 years ago
来源于pure function。
A pure function is a function where the return value is only determined by its input values, without observable side effects.
①不改变input ②类似数学中函数y=f(x), x确定,y就唯一确定。
React.PureComponent is exactly like React.Component but implements shouldComponentUpdate() with a shallow prop and state comparison.
——react.purecomponent
如何实现shouldComponentUpdate的呢? 大致是这样的
shouldComponentUpdate
if (this._compositeType === CompositeTypes.PureClass) { shouldUpdate = !shallowEqual(prevProps, nextProps) || ! shallowEqual(inst.state, nextState); }
提高React的速度。 此概念是相对于Component提出的。如何提高了速度呢? mount的时候Component中的shouldComponentUpdate始终返回true。
Component
true
PureComponent
state
props
render
更少的VirtualDOM和更少的diff,提高了React的速度。
下图为React更新的示意图,从左到右,update内容逐渐减少。
class FullName extends Component { render() { const { firstName, lastName } = this.props; return ( <div>{firstName} {lastName} - {new Date}</div> ) } }
context
Why and How to Use PureComponent in React.js
Разбор: как и зачем применять PureComponent в React(看评论)
什么是 Impure Component
How to implement shouldComponentUpdate with this.context? #2517
React PureComponent 源码解析
[]()
1.概念来源
来源于pure function。
①不改变input ②类似数学中函数y=f(x), x确定,y就唯一确定。
2.概念
——react.purecomponent
如何实现
shouldComponentUpdate
的呢? 大致是这样的3.目的
提高React的速度。 此概念是相对于Component提出的。如何提高了速度呢? mount的时候
Component
中的shouldComponentUpdate
始终返回true
。PureComponent
之后,通过判断state
、props
有没有改变,来决定是否render
,render
,那么VirtualDOM就不会生成,更少的VirtualDOM和更少的diff,提高了React的速度。
下图为React更新的示意图,从左到右,update内容逐渐减少。![rendering-process](https://cloud.githubusercontent.com/assets/717817/24574439/2f8c02e6-16c5-11e7-9b19-c7b26e53a071.gif)
4.什么不是PureComponent
遗留问题
context
对PureComponent有什么影响?参考文献
Why and How to Use PureComponent in React.js
Разбор: как и зачем применять PureComponent в React(看评论)
什么是 Impure Component
How to implement shouldComponentUpdate with this.context? #2517
React PureComponent 源码解析
[]()