"Classes are the future of JavaScript because of React."
I heard this many times over the last few years, and I remained highly dubious because, I believed, use of classes in JavaScript was perfectly fine... until you understood JavaScript. Once you do that, using classes becomes clunky, uncomfortable, and awkward.
Finally, the voices of reason and "hey, let's use JavaScript the way it was intended" have been heard.
Hooks is functional composition well executed, and I finally feel comfortable using React.
When I'm in C#, I use classes/interfaces in a composition-based architecture because it's what the language is intended to be based on, and I love it.
When I'm in JavaScript, I use functions in a composition-based architecture because it's what the language is intended to be based on, and I love it.
Top comments (16)
I disagree that hooks are "functional composition well executed".
The engine internally counts calls to establish the identity of individual states and effects - it requires a custom linter to validate that valid JavaScript is also valid React.
To me, this goes against the idioms of the language - it's an attempt to make something that evokes feelings of pure functional composition, when in fact it's largely a language "hack".
I find it by all means far more unnatural and further from the language than classes.
Not saying that classes are or were a great way solve these problems, but at least it was leveraging the language rather than trying to subvert it.
I hope we'll see more UI frameworks in the future that really compose functions - without the hacks - the way React hooks appear to do until you understand what's really going on beneath the hood. 😐
I disagree. I think over the years many JavaScript developers found clunky ways to mimic the behavior of classes while refusing to name them which, for the sake of being functional (looking at you module pattern). Classes absolutely have a role, especially in such a poorly designed language, with such a huge ecosystem.
Hooks in react are a great edition, but I don't think they make classes obsolete.
Heard of Web Components?
The class syntax is there.
But if you don't like syntactical sugar, we could always make it "JavaScript".
Bottom line, do what works best for you.
Right? Lets move away from react and all frameworks/libs and the dependencies and patterns they force on us.... Same as jQuery, these will also become obsolete and we'll be back to vanilla. Start the movement to liberal JS and web components today!
Well said. I would even recommend doing a js-benchmark comparison of invoking multiple methods on a class instance vs the same methods composed into an object directly (individual functions as fields in an object directly). The syntactic sugar of post-ES2015 classes can't make up for such a drastic performance drop. But this is one big reason (in JavaScript) to favor composition over inheritance.
Languages like Java are built on top of a JVM highly optimized for object oriented programming (C# too, although it isn't as bare metal fast these days as Java). JavaScript meanwhile is a LISP/Scheme/SmallTalk clone with some "C"-like syntax.
Lots of people spend their week tinkering with the many compile-to-JS solutions and I wish them great enjoyment in their efforts. I just hope they don't spend their entire careers coding JavaScript from their favorite abstraction layer and never get the opportunity to see all the low-level tricks and simplicity of vanilla js.
Question: How are folks debugging React in the browser when most code is transpiled (God I hate that word).
React Developer Tools for Chrome
But how are you debugging the raw JS?
Source Maps?
👆
Imho any discussion of JavaScript classes without a mention of Typescript is a bit hasty.
You have a lot to learn
Yes.
This post is so opinionated and poorly written.
The quote "Classes are the future of JavaScript because of React." is completely nonsense.
React was never the reason why people began to use classes, nor why they were even put in the ES spec.
Classes are "natively" supported, so how can they not be intended usage?
There's literally no context in this absurd post at all, as everything gets switched around.
Are we talking about React only, Node.js, ES3, ES6 or transpilers?
I mean what's the point?
Out of curiosity ... do you use vanilla JavaScript? Or some typed flavour?
So many people in the comments disagreeing but I definitely agree with you
I do not mind at all when people disagree with me. They are allowed to be wrong.