React - 偏好:函数组件与类组件 [英] React - Preference: function component vs class component

查看:43
本文介绍了React - 偏好:函数组件与类组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类组件是否被废弃了?

我看到在一些库示例中,函数组件是优先考虑的.

I see that in several libraries examples have function components as a priority.

尤其是 React 导航.

Especially React Navigation.

同样,React 本身与 Hooks 只使它们可用于函数组件.

Likewise, React itself with Hooks only makes them available for function components.

主要问题是:为什么功能组件如此重要?

The main question is: Why are function components being so prioritized?

推荐答案

不,我认为今天不会放弃类组件.也许在未来.

No, i think Class Components won't be abandoned today. Maybe in future.

它们不像功能组件那样轻量级,但我在社区上看到很多使用类组件的项目.

They aren't lightweight as Functional Components can be, but i see a lot projects on community using Class Components.

然而,这里我们有一些社区支持 Functional Components 方法的原因:

However, here we have some reasons why the community is supporting the Functional Components approach:

  • 类组件需要更多代码,但也会给您带来一些好处,您稍后会看到(Babel 转译的代码也会更大)
  • 函数式组件只是一个普通的 JavaScript 函数,它接受 props 作为参数并返回一个 React 元素.
  • 函数式组件更易于阅读和测试,因为它们是纯 JavaScript 函数(代码更少).
  • React 团队提到 在未来的 React 版本中可能会有功能组件的性能提升
  • Class Components requires more code but will also give you some benefits which you will see later on (the transpiled code by Babel will be larger too)
  • A functional component is just a plain JavaScript function which accepts props as an argument and returns a React element.
  • Functional component are much easier to read and test because they are plain JavaScript functions (less code).
  • The React team mentioned that there may be a performance boost for functional component in future React version

看到这个答案:https://stackoverflow.com/a/49613435/4119452

更多信息:https://www.twilio.com/blog/反应选择功能组件

这篇关于React - 偏好:函数组件与类组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆