我发现React docs有点不一致。在Components and Props部分,他们通过功能性 Welcome
组件的示例解释了
React使用
Welcome
作为道具调用{name: 'Sara'}
组件。
考虑到组件是纯函数,这是公平的。然后在State and Lifecycle中,他们通过类 Clock
组件的示例提及
React调用
Clock
组件的构造函数。 [...]然后React调用Clock组件的render()
方法。
通过阅读文档以及this Medium post,使用功能组件,
props
对象基于类组件的流程不同:
根据文章,最大的区别在于"功能组件没有实例",这意味着它们是直接调用的。 React DOM"只是使用函数的调用来确定为函数提供的DOM元素"。
但是,这与其他来源无关。例如,here和here Dan Abramov提到功能组件是内部类。换句话说,React只是将一个功能组件包装到一个类中并实例化它,就像它是一个类组件一样。 Another article甚至说前者甚至比后者慢。
答案 0 :(得分:1)
Are functional components converted into class components by 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.
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. Read this to gain more understanding.
Should I really sacrifice my workflow
I guess not