Javascript 我可以在PureComponent中使用shouldComponentUpdate吗
我知道Javascript 我可以在PureComponent中使用shouldComponentUpdate吗,javascript,reactjs,Javascript,Reactjs,我知道shouldComponentUpdate和PureComponent的功能。但是我想知道我是否可以同时使用这两个 假设我有很多道具,我想让浅比较处理PureComponent。除了1个道具,需要比较一下。那么,是否可以为其使用shouldComponentUpdate?哪个反应会考虑?< /P> 换句话说,将响应调用PureComponent的浅层比较,然后调用我的shouldComponentUpdate?或者我的shoulldcomponentupdate会覆盖原始组件 如果它的两层
shouldComponentUpdate
和PureComponent
的功能。但是我想知道我是否可以同时使用这两个
假设我有很多道具,我想让浅比较处理PureComponent
。除了1个道具,需要比较一下。那么,是否可以为其使用shouldComponentUpdate
?哪个反应会考虑?< /P>
换句话说,将响应调用PureComponent
的浅层比较,然后调用我的shouldComponentUpdate
?或者我的shoulldcomponentupdate
会覆盖原始组件
如果它的两层,如果
PureComponent
返回false,那就好了,控件进入我的shouldComponentUpdate
中,我有另一个机会返回false
您将首先在开发环境中得到一个警告,以检查在处理PureComponent
时是否定义了该方法:
if (
isPureComponent(Component) &&
typeof inst.shouldComponentUpdate !== 'undefined'
) {
warning(
false,
'%s has a method called shouldComponentUpdate(). ' +
'shouldComponentUpdate should not be used when extending React.PureComponent. ' +
'Please extend React.Component if shouldComponentUpdate is used.',
this.getName() || 'A pure component',
);
}
然后,在渲染时,如果定义了此方法,则实际上
甚至不检查组件是否为PureComponent
,而是使用您自己的实现
if (inst.shouldComponentUpdate) {
if (__DEV__) {
shouldUpdate = measureLifeCyclePerf(
() => inst.shouldComponentUpdate(nextProps, nextState, nextContext),
this._debugID,
'shouldComponentUpdate',
);
} else {
shouldUpdate = inst.shouldComponentUpdate(
nextProps,
nextState,
nextContext,
);
}
} else {
if (this._compositeType === ReactCompositeComponentTypes.PureClass) {
shouldUpdate =
!shallowEqual(prevProps, nextProps) ||
!shallowEqual(inst.state, nextState);
}
}
因此,通过在
PureComponent
上实现您自己的shouldComponentUpdate
,您将失去肤浅的比较。我还能在哪里检查我的组件是否被渲染?