我想在React es6组件中定义静态。我知道它是如何完成以下组件
var MyComponent = React.createClass({
statics: {
customMethod: function(foo) {
return foo === 'bar';
}
},
render: function() {
}
});
但是对于下面定义的反应组件
需要相同的class MyComponent extends Component{ ... }
此外,我想从MyComponent
将要实例化的地方调用该方法。
答案 0 :(得分:5)
您可以使用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'