React 类组件与功能组件

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!