我试图了解何时使用 React 功能组件与类,并阅读 docs他们并没有真正深入细节。你能给我一些下面的主要例子吗?当你想要一个类的特定功能来制作一个组件时?
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.
最佳答案
2022 年更新
您只需要一个 class
组件,当您:
- 需要创建一个 error boundary使用
componentDidCatch
2016 年原始答案
您只需要一个 class
组件,当您:
- 需要组件状态或
- 需要生命周期方法。比如
componentDidMount
等
关于javascript - React 功能组件 vs 经典组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38926574/