用ES6类反应静态 [英] React statics with ES6 classes

查看:113
本文介绍了用ES6类反应静态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class SomeComponent extends React.Component {

render(){
// ...
}

}

React.statics = {
someMethod:function (){
// ...
}
};

上面的东西给了我未定义的方法 someMethod 当我做 SomeComponent.someMethod()

解决方案

statics 只适用于 React.createClass 。简单地将方法声明为静态类方法:

  class SomeComponent extends React.Component {

static someMethod(){
// ...
}

render(){
// ...
}

}






关于

  React.statics = {...} 

您在 React 对象上创建一个静态属性。该属性不会神奇地扩展您的组件。


Does the statics object work with ES6 classes in React?

class SomeComponent extends React.Component {

  render() {
    // ...
  }

}

React.statics = {
  someMethod: function() {
    //...
  }
};

Something like the above gives me undefined method someMethod when I do SomeComponent.someMethod()

解决方案

statics only works with React.createClass. Simply declare the method as a static class method:

class SomeComponent extends React.Component {

  static someMethod() {
    //...
  }

  render() {
    // ...
  }

}


Regarding

React.statics = { ... }

You are literally creating a statics property on the React object. That property does not magically extend your component.

这篇关于用ES6类反应静态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆