Back in February 2019, React 16.8. introduced Hooks, a new way of using state and other React features without writing a class.
As of November 2020, the class syntax is still supported, and React official documentation states there are no plans to remove classes from React.
Do you still maintain some code where you have to use React classes? What about the projects you create from the ground-up?
Latest comments (26)
obviously
Thanks for the input πβ€
I just use both, depends on my mood.
Ever since Hooks were introduced, I have used them instead of classes. For me, I like the way
useStateinitializes and updates the state. This is a personal preference, but I like using functional components more than classes. This does not mean I have a problem with class-based components or hate them.I do use classes occasionally. Especially when something needs to be done after a component has been unmounted or when the component is updated. I still can't get around to using
useEffectwell. I findcomponentWillUnmountandcomponentDidUpdateeasier to use.As for which one is better, I think both get the job done when it truly matters. It all depends on whether a library needs classes or the developer prefers one over the other.
yeah!
Classes are dead to me π
I don't use classes anymore because I want my code to be as modern as possible and I don't want to stay on the "old" technology, because 1) there would be no development in technology otherwise and 2) I wouldn't learn anything new.
Classes may not be a "modern" concept to React land, but classes are anything but unmodern. Classes are part of the ECMAScript specification, they are very much a real and implemented language-level feature. The React team spread a lot of dangerous and unsubstantiated rhetoric when they blamed classes for problems caused by their ineptitude.
I ocasionally do, although it's gotten more and more annoying and useless with the largest amount of development trending towards plain functions everywhere.
The thing I miss the most is the general idea of knowing exactly on which part of the lifecycle I was on any given code block. Been thinking for a while about making a bunch of lifecycle hooks, as in, actual hooks that look like React Hooks and map to different points in a component's life; that way we could regain some of that sense of control, even if it's only useful for dev experience.
I do have quite a few class based components laying around in my code, but I've almost exclusively used functional components ever since hooks released. I don't go out of my way to refactor class based components into their functional counterparts, but I won't write a class based one unless it's absolutely necessary. There are a few packages I depend on that require class based components to function properly. The use case is so far and few between though.
To be honest with you, when I'm asked to make a change to a particular feature and I open up the file and see a big ol class... it scares me a little.
I'm writing new code with hooks. My old projects still have classes. They work perfectly fine, why waste time changing them? In general I'd only convert working code when the benefits outweigh the cost.
I've never really liked having two types of components. I've had several cases when adding a new functionality meant moving the state up or down the hierarchy. Refactoring a class component into a function component (or vice versa) is a pain. At least with hooks it's not an issue anymore. I'm not 100% comfortable with the magic behaviour that come with hooks but I like having code that refactors easily.
I use Hooks everywhere.