简体   繁体   English

将ES6类语法用于React组件

[英]Using ES6 class syntax for react components

As far as I am aware there are two ways to create and export a react class using ES6. 据我所知,有两种方法可以使用ES6创建和导出React类。

Maybe considered the old way: 也许被认为是老方法:

export default React.createClass({
   ...
});

Maybe considered the new way: 也许考虑了新方法:

export class Todo extends React.Component {
   ...
};

I was asked today why I prefer the 'old' way compared to the 'new' way. 今天有人问我为什么比“新”方式更喜欢“旧”方式。 I didn't really have a good answer apart from the fact that the 'new' way uses extends, and as a result, inheritance. 除了“新”方式使用扩展以及继承这一事实外,我并没有一个很好的答案。 I was taught in c# to always favour composition over inheritance. 我在C#中被教导要始终偏重于继承而不是继承。 And in javascript the same, preferring to use the revealing module pattern over prototypical inheritance for 95% of cases. 而且在javascript中,在95%的情况下,相对于原型继承,更喜欢使用显示模块模式。

Being fairly new to react, my question is, am I missing something to why the new way is better than the old way? 我的反应很新,我的问题是,我是否缺少一些理由来说明为什么新方法比旧方法更好? In my eyes it is far cleaner to have a function that accepts parameters and returns an object compared to inheriting from one. 在我看来,与从一个对象继承相比,拥有一个可以接受参数并返回对象的函数要干净得多。

Would it maybe not be nicer to follow a conversion, TodoComponent, maybe, or add some sort of decorator rather then extending from React.Component? 跟随转换,例如TodoComponent,或者添加某种装饰器,而不是从React.Component扩展,会不会更好?

I tend to always look at the React team examples for guidance. 我倾向于总是查看React团队的示例以获取指导。 In this case, they've provided a whole example dedicated to ES2015. 在这种情况下,他们提供了一个专门针对ES2015的示例 That clearly covers the "new" syntax, as you refer to it. 正如您所引用的那样,这显然涵盖了“新”语法。

I also peruse a lot of code looking at how things are done. 我还细读了很多代码以了解事情的完成方式。 Most projects using ES2015 are using the "new" syntax as well. 大多数使用ES2015的项目也都使用“新”语法。

I can't tell you which is better, I'm in the middle. 我不能告诉你哪个更好,我在中间。 But the one thing that you have to keep in mind is that the majority will go with the new syntax, especially as ES6 becomes more available. 但是您要记住的一件事是,大多数将使用新语法,尤其是随着ES6的可用性越来越高。 You should get used to seeing it and using it because you will see more and more examples of it in the future while people are migrating to it. 您应该习惯于查看和使用它,因为将来在人们迁移到它时,您将看到越来越多的示例。 It's almost a necessity to stay relevant and not fall back. 保持相关性并且不退缩几乎是必要的。 You will get used to it, I'm actually starting to like it more and more. 您会习惯的,我实际上已经开始越来越喜欢它。

The ES6 syntax will be transpiled into ES5 as modern browser do not fully support ES6 yet. 由于现代浏览器尚不完全支持ES6,因此ES6语法将被移植到ES5中。 One benefit of the old way would be that [mixins] can only be used the old way. 旧方法的一个好处是[mixins]只能以旧方法使用。 The new way will result in less typing and a few extra functions. 新方法将减少打字和一些额外的功能。 Especially scoping through arrow functions! 特别是通过箭头功能进行作用域! Checkout: https://facebook.github.io/react/blog/2015/01/27/react-v0.13.0-beta-1.html#mixins 结帐: https : //facebook.github.io/react/blog/2015/01/27/react-v0.13.0-beta-1.html#mixins

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM