反应:为什么静态 propTypes [英] react: why static propTypes

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

问题描述

我正在查看 redux todomvc 代码.static propTypes 中的 static 关键字是什么?谢谢

I am looking the redux todomvc codes. What is the static keyword in static propTypes? Thanks

更新

不知道为什么投反对票?这个帖子是不是太简单了?欢迎提出意见.谢谢.我希望我可以删除这篇文章.

No idea why downvoted? Is this post too simple? Comments are welcomed. Thanks. I hope I can delete this post.

推荐答案

static 不是上一代 Javascript(ES5")的一部分,这就是为什么你不会在较旧的文档.但是,除了 Internet Explorer (http://caniuse.com/#search=es6),如果你使用像 Babel 这样的转译器,你可以在任何浏览器中使用它.大多数 React 用户已经在使用 Babel 来转译他们的 JSX,所以 React 站点(如 Redux TodoMVC)认为这是理所当然的.您可以在此处static 的更多信息>https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static.

static was not part of the last generation of Javascript ("ES5"), which is why you won't find it in older documentation. However it, and the rest of the "ES6" class syntax is now supported in all major browsers except Internet Explorer (http://caniuse.com/#search=es6), and if you use a transpiler like Babel you can use it in any browser. Most React users are already using Babel to transpile their JSX, so React sites (like Redux TodoMVC) take it for granted. You can read more about static here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static.

static propTypes的情况下,propTypes需要在类本身上声明,而不是在类的实例上.换句话说,如果你使用无状态组件:

In the case of static propTypes, propTypes need to be declared on the class itself, not on the instance of the class. In other words, if you use stateless components:

function Foo() { 
    this.PropTypes = somePropTypes; // bad
    return <div></div>;
}
Foo.PropTypes = somePropTypes; // good

当使用 ES6 类时,等价于 Foo.PropTypes = somePropTypes 是:

When using ES6 classes, the equivalent of Foo.PropTypes = somePropTypes is:

class Foo extends React.Component {
    static PropTypes = somePropTypes;
}

作为旁注,在任何浏览器中都不存在在类中定义属性的能力(目前):您需要一个转译器,例如带有 transform-class-properties 的 Babel插件.

As a side note, the ability to define properties in a class like that doesn't exist in any browser (yet): you need a transpiler such as Babel with the transform-class-properties plugin.

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

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