Reaction to Javascript Classes
My journey to using react again
I am going to start off with this is 100% an opinion piece. If someone sends you this blog post and trys to say you are doing it wrong they are not reading this content correctly. Keep doing what you feel is right. There is a million ways to solve problems and my way is just one of many. Now with that out of the way.
Recently I been getting an itch to build something. This always happens every few months and normally I dont make it past proof of concept phase. Well The itch I got this time was to understand and build a PWA. I might make a seperate post on that later on what I found and how I got through some challenges. Anyway I quickly discovered that PWA’s are mostly for already built web apps that are trying to be setup for offline support and get access to more native apis on phones. So then I started thinking about what kind of app I would like to build. Got a few ideas but my front end side of things is pretty much jQuery and templates. I never got deeper than that because the companies I have worked for that was the stack. Back when I was getting excited about javascript and all the things being built with JS, React had been in beta and was just coming out. I learned it back then but never really used it on anything other then a simple todo app (to learn it) and I gave a couple talks on it at a local meetup. Well I did not keep up with it and became disconnected from that community.
2020 holiday season has been slow due to the pandemic going on which has got me in a building mood. Well I decided that I wanted to build a SPA with no backend (mostly to see if I can). Well I looked at Vue and Svelte but something in me wanted to go back to React. Maybe because I thought I already knew it or something. Well going through the tutorial again and building some pieces of componets in it I quickly realized why I never stayed with it. The complexity they introduce in staggering. You had function components that I liked because they were simple and easy to use. Then you have class components that add state. Now state is where react becomes a pain. People try doing it the react way but then the waterfall of components to get state where it needs becomes combersome. Then you have people that bring in some state management engine like Redux to make it more event based and have flags getting flipped by events and what not. This all just seems silly.
Hooks saved React for me
I will be the first to admit when I think something is too complex. I was about to give on react a second time. Then I listened to a podcast and in a very small section of the podcaster mentioned react hooks and was excited but cut it off and said they could do an episode on just the hooks alone. I went and dug in and found out react hooks can bring state and other useful features to pretty much anything in React. Though the power comes in for function components. So one the things that use to be annoying about React is I would want to use function components because I liked them more than classes but the only way to use state was to be a class so in fear of ever having to convert a function component to a class one left me always making class components but it was so much code for when I would just want a collection of small components. I watched a talk from back in 2018 when they first started showing off hooks and they blew my mind. I linked the video below. Please go check that out to see the power of hooks.
Now hooks dont solve the waterfall of state problem. It just makes state more injectable. Also make other effects and logic more injectable. Dont need a ton of syntax and code to accomplish the same level of funcationality. Though figuring out which component needs the state and to manage it is still a challenge but at least we dont wrap a million components to get state somewhere but we will likely still wrap a few hundred components to achieve the same results.
Now my bone to pick with javascript classes
Javascript classes arent really new anymore but I remember when React was coming out they were the new hawtness. I remember drooling over them. Though they were what scared me away from React and probably other frameworks and I am just now seeing that. In my opinion javascript classes just dont make sense. The language has concepts of inheritance but no interfaces and everything else that makes classes work. Javascript is an object orianted languge but it handles things derastically different then other languages. Classes are just syntactic sugar. If you wanna use them by all means go right on a head. Though I feel they are masking and not really helping people understand how objects and inheritance in javascript really works. They just muddy the waters. Also you end up writing so much code to solve simpleist issues.
Whole languages have come out to add the missing parts to make classes in javascript make sense. Typescript exists for this reason. Now if you wanna use classes in javascript my recommendation is dont use vanilla javascript, go take a look at typescript and that community and libraries that are produced for it. Now if you wanna use vanilla javascript what you should be checking out is closjures, prototypes, and async programming concepts (the latter is useful to know anyway) and everything linked with those. A javascript module built without classes can still be beautiful if you just take the time to learn how to build it and make it clean. Like take React hooks for example, they can solve all the same issues as class components with way less code and magic.
I come from languages where class type objects are front and center to be used and they make sense there for example C++ and PHP have all the tools to make class based programming make sense. Javascript they just feel so shoe horned in that they just add clutter and chaos. Try a more functional approach and you might see what I am talking about. Though you as another person on this planet probably have another opinion and that is fine. I will respect your opinion and thank you for reading mine.