Reactjs 如何覆盖react组件生命周期方法

Reactjs 如何覆盖react组件生命周期方法,reactjs,Reactjs,我希望覆盖react组件生命周期(如shouldComponentUpdate),并在必要时调用默认生命周期。我试着从以下内容开始: shouldComponentUpdate(nextProps, nextState) { return super.shouldComponentUpdate(nextProps, nextState); } 但它不起作用。我遗漏了什么?我认为最好的方法是编写es7 decorator,你可以这样写: function shouldComponentU

我希望覆盖react组件生命周期(如shouldComponentUpdate),并在必要时调用默认生命周期。我试着从以下内容开始:

shouldComponentUpdate(nextProps, nextState) {
    return super.shouldComponentUpdate(nextProps, nextState);
}

但它不起作用。我遗漏了什么?

我认为最好的方法是编写es7 decorator,你可以这样写:

function shouldComponentUpdate(nextProps, nextState) {
  //your custom shouldComponentUpdate function
}

function customShouldComponentUpdate(component) {
  component.prototype.shouldComponentUpdate = shouldComponentUpdate;
  return component;
}
感谢这个装饰程序,您可以覆盖react组件中的方法
shouldComponentUpdate

用法示例:

@customShouldComponentUpdate
class Foo extends Component{
  //code
}

我认为最好的方法是编写es7 decorator,你可以这样写:

function shouldComponentUpdate(nextProps, nextState) {
  //your custom shouldComponentUpdate function
}

function customShouldComponentUpdate(component) {
  component.prototype.shouldComponentUpdate = shouldComponentUpdate;
  return component;
}
感谢这个装饰程序,您可以覆盖react组件中的方法
shouldComponentUpdate

用法示例:

@customShouldComponentUpdate
class Foo extends Component{
  //code
}

您如何传输代码?当我使用正确的es6转换+多边形填充的babel时,这对我来说很有用。还要阅读以下内容:正如文档所说,React.Component是一个抽象类,因此没有可用的默认实现。您需要在组件中定义它。您如何传输代码?当我使用正确的es6转换+多边形填充的babel时,这对我来说很有用。还要阅读以下内容:正如文档所说,React.Component是一个抽象类,因此没有可用的默认实现。您需要在组件中定义它。