[英]Difference between React.createClass and <Name> extends Component
I've been through several tutorials and guides for React Native and have seen these two being used in almost the same way: 我已经看过关于React Native的一些教程和指南,并且看到这两种用法几乎相同:
var anyName = React.createClass({
render: function() {
return (
...
);
}
});
and 和
class anyName extends Component {
render() {
return (
...
)
}
}
Not sure what the difference between them is. 不知道它们之间有什么区别。
The first piece of code creates new JS object in old way (ES5). 第一部分代码以旧方式 (ES5)创建新的JS对象。 It simply creates plain JS object and then fill it with React methods and properties using
prototypes
. 它只是创建一个普通的JS对象,然后使用
prototypes
使用React方法和属性填充它。
React.createClass
method must be filled with object that implements render
method. React.createClass
方法必须填充实现render
方法的对象。 (You can read more about this method here . (您可以在此处阅读有关此方法的更多信息。
The second code uses newest JS standard (ES6) classes and inheritance . 第二个代码使用最新的JS标准(ES6) 类和继承 。 In fact
Component
is ReactComponent
and you can read more about it here . 实际上
Component
是ReactComponent
,您可以在这里阅读有关它的更多信息。
Please note that these two ways of initializing React Component will give you sightly different results. 请注意,这两种初始化React Component的方式将给您明显不同的结果。 For example you cannot use
mixins
at this time with ES6 inheritance and you have to bind
methods to parent object. 例如,您此时不能将
mixins
用于ES6继承,而必须bind
方法bind
到父对象。 You can read more about differences here . 您可以在此处阅读有关差异的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.