简体   繁体   English

功能组件是否已变成引擎盖下的类组件?

[英]Are functional components turned into class components under the hood?

I find a bit of a discrepancy in React docs. 我在React文档中发现了一些差异。 In the section on Components and Props , they explain, through the example of a functional Welcome component, that 在“ 组件和道具 ”部分中,他们通过功能性 Welcome组件的示例来说明:

React calls the Welcome component with {name: 'Sara'} as the props. React用{name: 'Sara'}作为道具来调用Welcome组件。

Fair enough, given that the component is a pure function . 考虑到组件是一个纯函数 ,这很公平。 Then in State and Lifecycle , they mention, through the example of a class Clock component, that 然后在State and Lifecycle中 ,他们通过 Clock组件的示例提到:

React calls the constructor of the Clock component. React调用Clock组件的构造函数。 [...] React then calls the Clock component's render() method. [...] React然后调用Clock组件的render()方法。

From reading the docs, as well as this Medium post , with functional components, 通过阅读文档以及这篇有关功能组件的中级帖子

  1. the component is invoked directly as a function with props object 直接使用props对象将该组件作为函数调用
  2. it returns a React element, ie an object that models the DOM element(s) to be rendered 它返回一个React元素,即模拟要渲染的DOM元素的对象
  3. React DOM then creates and inserts a DOM node to the "real" DOM 然后,React DOM创建一个DOM节点并将其插入“真实” DOM中

The flow is different with class-based components: 该流程与基于类的组件不同:

  1. the component class is instantiated and the object instance is stored in memory 实例化组件类,并将对象实例存储在内存中
  2. the render method is invoked and the React element (object) is returned 调用render方法并返回React元素(对象)
  3. the React element, backed by its class instance, is used by React DOM to generate and mount an actual DOM node 由其类实例支持的React元素被React DOM用于生成和安装实际的DOM节点

The big difference, according to the article, is that "function components don't have instances", meaning that they are invoked directly. 根据这篇文章,最大的区别是“功能组件没有实例”,这意味着它们可以直接调用。 React DOM "just uses the invocation of the function to determine what DOM element to render for the function". React DOM“仅使用函数的调用来确定要为该函数呈现的DOM元素”。

However, this is incogruent with other sources. 但是,这与其他来源不一致。 For instance, here and here Dan Abramov mentions that functional components are classes internally. 例如, 在这里此处 Dan Abramov提到功能组件是内部类。 In other words, React will just wrap a functional component into a class and instantiate it, as if it were a class component. 换句话说,React会将功能组件包装到一个类中并实例化它,就好像它是一个类组件一样。 Another article goes as far as saying that the former are even slower than the latter. 另一篇文章甚至说前者比后者慢。

Question

  • Are functional components converted into class components by React? React是否将功能组件转换为类组件?
  • Is there (yet) any performance benefits to using functional, rather than class components? 使用功能组件(而不是类组件)是否有任何性能优势? (eg here it says no benefits yet, I assume before React Fiber?) (例如, 在这里它还说没有好处,我认为在React Fiber之前?)
  • Should I really sacrifice my workflow, if I could have state across the app, where it would logically fit in, eg a form/controlled component somewhere deep down the tree? 如果我可以在整个应用程序中拥有状态,那么我是否应该真正牺牲我的工作流程,使其在逻辑上适合放置在哪里,例如,树深处的表单/受控组件?

Are functional components converted into class components by React? React是否将功能组件转换为类组件?

There is no “optimized” support for them (functional component) yet because stateless component is wrapped in a class internally. 尚无针对它们(功能组件)的“优化”支持,因为无状态组件内部包装在类中。 It's same code path. 这是相同的代码路径。

From a twitter thread by Dan Abramov. 从Dan Abramov的推特线程中获取。

Is there any performance benefits to using functional, rather than class components? 使用功能组件而不是类组件是否对性能有好处?

Apparently right now there is no performance benefits because React does a lot of things on Functional Components which decreases performance. 显然,目前还没有性能优势,因为React在功能组件上做了很多事情,从而降低了性能。 Read this to gain more understanding. 阅读本文以获得更多理解。

Should I really sacrifice my workflow 我真的应该牺牲我的工作流程吗

I guess not 我猜不会

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

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