[英]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'srender()
method.[...] React然后调用Clock组件的
render()
方法。
From reading the docs, as well as this Medium post , with functional components, 通过阅读文档以及这篇有关功能组件的中级帖子 ,
props
object props
对象将该组件作为函数调用 The flow is different with class-based components: 该流程与基于类的组件不同:
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.
另一篇文章甚至说前者比后者慢。
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.