React 类组件与功能组件 [英] React Class components vs Functional components
问题描述
在 React16
之前,只要需要使用 state
或生命周期方法
在你的组件中.在最新版本中,react 引入了 Hooks 和 Effects 使用它我们可以在我们的 state
和 lifecycle methods
中使用我们的 >功能组件代码>.
Before React16
, class components
were used over functional components
whenever there is a need to use state
or lifecycle methods
in your component.
In the latest release react introduced Hooks and Effects using which we can use state
and lifecycle methods
inside our functional components
.
那么类组件
还有什么理由存在吗?在任何情况下,我们仍然必须选择 class components
而不是 functional
So is there any reason why still the class components
exists? Is there any cases we still have to choose class components
over functional
推荐答案
这里 你可以看到需要类组件的情况
Here you can see the cases where you will need class components
需要类组件的最常见情况是,如果您想制作一个 ErrorBoundary
,你需要一个类组件,因为你不能用钩子实现 componentDidCatch
......还没有!
The most common case where you will need class components is if you want to make an ErrorBoundary
, you will need a class component because you can't implement componentDidCatch
with hooks... yet!
这篇关于React 类组件与功能组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!