2016-07-14 3 views
2

React es6 구성 요소에서 통계를 정의하고 싶습니다. 나는 또한 내가 MyComponent 인스턴스화 될 것입니다 장소에서 해당 메소드를 호출 할 구성 요소React es6 컴포넌트에서 statics를 정의하는 방법은 무엇입니까?

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

이하에 대해 수행하는 방법은 알고 있지만

class MyComponent extends Component{ ... } 

다음과 같이 정의 된 반응 구성 요소에 대해 동일합니다.

답변

5

당신은 ES6 클래스의 정적 멤버 변수를 만들 수 static 키워드를 사용할 수 있습니다

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

관련 문제