React 类组件与功能组件 [英] React Class components vs Functional components

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

问题描述

React16 之前,只要需要使用 state生命周期方法 在你的组件中.在最新版本中,react 引入了 HooksEffects 使用它我们可以在我们的 statelifecycle 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屋!

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