React functional components vs classical components(React 功能组件与经典组件)
问题描述
我试图了解何时使用 React 功能组件与类并从 docs 他们并没有真正深入细节.您能否给我一些下面的主要示例,说明您何时希望类的特定功能来制作组件?
I'm trying to understand when to use React functional components vs. classes and reading from the docs they don't really go into detail. Can you give me some primary examples of the below of when you would want a specific feature of a class to make a component?
功能性组件功能较弱,但更简单,其作用类似于只有一个 render() 方法的类组件.除非你需要仅在类中可用的功能,我们鼓励您使用功能组件.
A functional component is less powerful but is simpler, and acts like a class component with just a single render() method. Unless you need features available only in a class, we encourage you to use functional components instead.
推荐答案
你只需要一个 class
组件,当你:
You only need a class
component when you:
- 需要组件 state 或
- 需要生命周期方法.如
componentDidMount
等
- need the component state or
- need the lifecycle methods. such as
componentDidMount
etc.
这篇关于React 功能组件与经典组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:React 功能组件与经典组件


基础教程推荐
- 每次设置弹出窗口的焦点 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01