2016-07-14 6 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

関連する問題