Hi I'm very new in React. I've started learning React in the past 2 weeks, and I'm currently making a website using the MERN stack with a friend. I've been using class components whenever I saw a need for maintaining states, but I just discovered that hooks can mimic React classes and supposedly reduces code length, increases readability, and creates more maintainable code. So I'm just wondering, should class components be used in React as of 2020 when hooks exist? What use cases do classes cover that aren't covered by functional components?
-
Does this answer your question? [When to use ES6 class based React components vs. functional ES6 React components?](https://stackoverflow.com/questions/36097965/when-to-use-es6-class-based-react-components-vs-functional-es6-react-components) – Maddy Blacklisted Jun 19 '20 at 17:31
2 Answers
Hooks are meant as a complete replacement for class components. From react docs,
We intend for Hooks to cover all existing use cases for classes, but we will keep supporting class components for the foreseeable future.
Reading the page linked above about the adoption strategy, the intent behind hooks - would be a great place to understand and form an opinion.
Personally, I love hooks. And haven't used class components since hooks were introduced. I am yet to find a use-case where I needed to use a class component because hooks couldn't satisfy the ask.

- 758
- 5
- 12
it's preference thing but the community is moving towards hooks. Hooks + other features can pretty much cover everything done in hooks
e.g.
useEffect
=> componentDidMount, componentDidUpdate, componentWillMount
useState
=> this.setState({})
React.memo
=> shouldComponentUpdate
some things are easier in classes than they are in hooks at the moment. like the second argument in setState
is a callback to ensure that state is changed before executing. this is possible in hooks but it's not quite as clean as that

- 7,181
- 10
- 39
- 86