静态方法在 ES6 类 中未定义,在 reactjs 中带有装饰器

Static method is undefined in ES6 classes with a decorator in reactjs

我有一个带有装饰器的 ES6 class。它有一个静态方法 foo。但是,当我尝试访问静态方法时,它是未定义的。

@withStyles(styles)
class MyComponent extends Component {
    static foo(){
        return "FOO";
    }
    render(){
        var x = MyComponent.foo; // x=undefined
    }
}

当我删除装饰器时,我可以访问静态方法。它不再是未定义的。

class MyComponent extends Component {
    static foo(){
        return "FOO";
    }
    render(){
        var x = MyComponent.foo; // x=foo()
    }
}

这个问题有解决方法吗?

如果您将 babeles6 一起使用,它可以像这样被转译(到 es5):

var MyComponent = (function () {
  function MyComponent() {
    _classCallCheck(this, _MyComponent);
  }

  _createClass(MyComponent, null, [{
    key: 'foo',
    value: function foo() {
      return "FOO";
    }
  }]);

  var _MyComponent = MyComponent;
  Foo = withStyles(MyComponent) || MyComponent;
  return MyComponent;
})();

所以它的问题是 withStyles(MyComponent) 将 return 另一个函数,它显然没有您为原始 class.

指定的静态方法