什么是浅比较

最近在看一个组件的时候看到了 React.pureComponent,官方文档中对他的描述是

1
由于PureComponent的shouldeComponentUpdate里,实际是对props/state进行了一个浅对比,所以对于嵌套的对象不适用,没办法比较出来。

shallowCopy

在 React 中,shouldComponentUpdate的实现方式为

1
2
3
if (this._compositeType === CompositeTypes.PureClass) {
shouldUpdate = !shallowEqual(prevProps, nextProps) || ! shallowEqual(inst.state, nextState);
}

可以看到 PureComponent 就是对 props 和 state 的前后状态做的一个浅比较。

Vue 组件的本质是什么 埃鲁德·基普乔格:成为传奇
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×