Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我可以在PureComponent中使用shouldComponentUpdate吗_Javascript_Reactjs - Fatal编程技术网

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
,您将失去肤浅的比较。

我还能在哪里检查我的组件是否被渲染?