如何在 React es6 组件中定义静态?

How to define statics in React es6 component?

我想在 React es6 组件中定义静态。我知道它是如何为下面的组件做的

var MyComponent = React.createClass({
  statics: {
    customMethod: function(foo) {
      return foo === 'bar';
    }
  },
  render: function() {
  }
});

但对于如下定义的 React 组件也需要相同的方法

class MyComponent extends Component{ ... }

另外我想从 MyComponent 将被实例化的地方调用那个方法。

您可以使用static关键字在ES6中创建静态成员变量类:

class StaticMethodCall {
    static staticMethod() {
        return 'Static method has been called';
    }
    static anotherStaticMethod() {
        return this.staticMethod() + ' from another static method';
    }
}
StaticMethodCall.staticMethod(); 
// 'Static method has been called'

StaticMethodCall.anotherStaticMethod(); 
// 'Static method has been called from another static method'

Source and more info on MDN