WitVault
WitVault

Reputation: 24140

How to define statics in React es6 component?

I want to define statics in React es6 component. I know how its done for below component

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

But want same for a react component defined as below

class MyComponent extends Component{ ... }

Also I want to call that method from the place where MyComponent is going to be instantiated.

Upvotes: 2

Views: 378

Answers (1)

TimoStaudinger
TimoStaudinger

Reputation: 42520

You can use the static keyword to create static member variables in ES6 classes:

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

Upvotes: 5

Related Questions