反应功能组件与经典组件 [英] React functional components vs classical components

查看:139
本文介绍了反应功能组件与经典组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解何时使用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类组件一样。除非您只需要在课程中使用
功能,否则我们建议您使用功能性的
组件。

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.


推荐答案

您只需要一个组件:


  • 需要组件状态

  • 需要生命周期方法。例如 componentDidMount 等。

  • need the component state or
  • need the lifecycle methods. such as componentDidMount etc.

这篇关于反应功能组件与经典组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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