简体   繁体   English

React.createClass 与 ES6 箭头函数

[英]React.createClass vs. ES6 arrow function

I'm new to React and trying to get a handle on syntax.我是 React 的新手并试图掌握语法。

I'm developing in a React 15 environment (using the react-starterify template) and have been using the syntax in VERSION 2 below, but, most of the examples and tutorials I find in Facebook's React pages are VERSION 1. What's the difference between the two and when should I use the one over the other?我正在 React 15 环境中开发(使用 react-starterify 模板)并且一直在使用下面版本 2 中的语法,但是,我在 Facebook 的 React 页面中找到的大多数示例和教程都是版本 1。两个,我什么时候应该使用一个而不是另一个?

VERSION 1版本 1

var MyComponent = React.createClass({
  render: function() {
    return (
      <ul>
        // some list
      </ul>
    );
  }
});

module.exports = MyOtherComponent;

VERSION 2版本 2

const MyComponent = () => (
  <ul>
    // some list
  </ul>
);

export default MyComponent;

The second code is a stateless functional component and is a new syntax/pattern for defining components as a function of props .第二个代码是一个无状态的函数式组件,是一种新的语法/模式,用于将组件定义为props的函数。 It was introduced in React v0.14.它是在 React v0.14 中引入的。

You can read more about it on the official React Blog, here , on the official React Documentation, here .你可以在官方 React 博客上阅读更多关于它的信息, 这里,在官方 React 文档上, 这里

These components behave just like a React class with only a render method defined.这些组件的行为就像一个 React 类,只定义了一个渲染方法。 Since no component instance is created for a functional component, any ref added to one will evaluate to null.由于没有为功能组件创建组件实例,因此添加到一个的任何 ref 将评估为 null。 Functional components do not have lifecycle methods, but you can set .propTypes and .defaultProps as properties on the function.函数式组件没有生命周期方法,但您可以将.propTypes.defaultProps设置为函数的属性。

This pattern is designed to encourage the creation of these simple components that should comprise large portions of your apps.此模式旨在鼓励创建这些应包含大部分应用程序的简单组件。 In the future, we'll also be able to make performance optimizations specific to these components by avoiding unnecessary checks and memory allocations.将来,我们还将能够通过避免不必要的检查和内存分配来针对这些组件进行性能优化。


  • What's the difference?有什么区别?

    This pattern is similar to the "traditional" one, except for the fact that you're using simple functions instead of methods defined in a class.这种模式类似于“传统”模式,不同之处在于您使用的是简单函数而不是类中定义的方法。 This can be useful when you want to extract functions out of the class (eg for readability and cleanliness sake).当您想从类中提取函数时,这会很有用(例如,为了可读性和清洁性)。

    One important thing to note is that a functional component is just that - a function .需要注意的一件重要事情是功能组件只是一个功能 It's not a class.这不是一个班级。 As such, there's no global this object.因此,没有全局的this对象。 This means that when you're doing a render you're basically creating a new instance of a ReactComponent , thus leaving out the possibility for these javascript objects to communicate with each other via some global this .这意味着当您进行render您基本上是在创建ReactComponent的新实例,从而排除了这些 javascript 对象通过一些全局this相互通信的可能性。 This also makes the use of state and any life-cycle methods impossible as a consequence.这也导致无法使用state和任何生命周期方法。


  • How does my app benefit from it?我的应用程序如何从中受益?

    Performance性能
    When you're using stateless functional components, React is clever enough to omit all "traditional" life-cycle methods, which turns out to be providing a fair amount of optimizations.当您使用无状态函数式组件时,React 足够聪明,可以省略所有“传统”生命周期方法,结果证明提供了大量优化。 The React team has stated that they are planning to implement further optimizations in the future for memory allocations and reducing the number of checks. React 团队表示,他们计划在未来进一步优化内存分配并减少检查次数。

    Adaptability适应性
    Because we're only talking about a function (and not a class), we don't need to worry about state , life-cycle methods, or other dependencies.因为我们只讨论一个函数(而不是一个类),所以我们不需要担心state 、生命周期方法或其他依赖项。 Given the parameters, the function will always give the same output.给定参数,该函数将始终给出相同的输出。 As such, it's very easy to adapt such components wherever we want, which turns out to also make testing easier.因此,可以很容易地在任何我们想要的地方调整这些组件,这也使测试变得更容易。

    With React's stateless functional components, each component can be easily tested in isolation.使用 React 的无状态功能组件,可以轻松地对每个组件进行隔离测试。 No mocking, state manipulation, special libraries, or tricky test harnesses are needed.不需要模拟、状态操作、特殊库或棘手的测试工具。

    Encourages best practices鼓励最佳实践
    React is often compared to the V of the MVC pattern because it's meant for creating views. React 通常与 MVC 模式的V相比较,因为它用于创建视图。 The "traditional" ways of creating components make it easy to "hack in" business logic (eg with state or ref ) into components that really should only handle render logic.创建组件的“传统”方式使得将业务逻辑(例如使用stateref )“侵入”到真正应该只处理渲染逻辑的组件中变得容易。 They encourage laziness and writing smelly code.他们鼓励懒惰和写臭代码。 However, stateless functional components make it nearly impossible to take such shortcuts and force the better approach.然而,无状态的功能组件几乎不可能采取这样的捷径并强制采用更好的方法。


  • When should I use the one over the other?我什么时候应该使用一个而不是另一个?

    Generally, using the new pattern is recommended whenever possible!通常,建议尽可能使用新模式! If you only need a render method, but no life-cycle methods or state , use this pattern.如果您只需要一个render方法,但不需要生命周期方法或state ,请使用此模式。 Of course, sometimes you do need to use state , in which case you're fine using the traditional pattern.当然,有时您确实需要使用state ,在这种情况下,您可以使用传统模式。

    Facebook recommends using stateless components whenever rendering static presentational components. Facebook 建议在渲染静态表示组件时使用无状态组件。 Then, if some kind of state is needed, simply wrap those in a stateful component to manage them by using its' state and sending down props to the stateless components.然后,如果需要某种状态,只需将它们包装在有状态组件中,以通过使用其state并将props发送到无状态组件来管理它们。

To be added, from react 15.5.0, React.createClass will be deprecated.添加,从 react 15.5.0 开始, React.createClass 将被弃用。 They recommend you to move to ES2015 class or use arrow function.他们建议您转到 ES2015 类或使用箭头功能。

When React was initially released, there was no idiomatic way to create classes in JavaScript, so we provided our own: React.createClass.在 React 最初发布时,没有在 JavaScript 中创建类的惯用方法,因此我们提供了自己的:React.createClass。

Later, classes were added to the language as part of ES2015, so we added the ability to create React components using JavaScript classes.后来,类作为 ES2015 的一部分添加到语言中,因此我们添加了使用 JavaScript 类创建 React 组件的功能。 Along with functional components, JavaScript classes are now the preferred way to create components in React.与功能组件一样,JavaScript 类现在是在 React 中创建组件的首选方式。

For your existing createClass components, we recommend that you migrate them to JavaScript classes.对于您现有的 createClass 组件,我们建议您将它们迁移到 JavaScript 类。

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

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