Ask HN: Why React?
I'm a self-taught programmer and stumbled my way into using htmx + alpine + django for a project. The usual case I hear for why to use react at all is because it manages state well, but even from my naive perspective I seem able to do what I need to do (handling several modes where functions vary based on those modes) using Alpine.store.
I know barely anything about react, but I'd love to get your takes as virtually all of you are more experienced than I am. Why react in general?
30 comments
[ 3.7 ms ] story [ 93.6 ms ] thread- People will pay me to build with it
- State - Update a value and all the places that uses the value get updated without any extra fiddling. If your pages are very dynamic or uses lots of use interaction you skip a lot of complexity
- Components - All components end up in their own file, adding reusability and clarity to the logic
- NPM packages - Easily add extra functionality
- JSX - It's a delight to write "HTML" and embed variables and functions directly where you need it
We have been writing complex standalone apps for decades without needing frameworks and they manage state just fine. At least for medium size GUIs, state management can be done with a few simple architectural conventions, MVC pattern, etc. May be I am missing something special to web applications...
It's very common the need to manage state in 3 different levels: tbe local one, the one shared between running instances/tabs and the server. It is not simple to keep them synced the right way if that's a requirement.
Most of your components in a React app should be pure functions, meaning their output for a combination of props should be the same every time. This makes testing extremely easy, although YMMV depending on how you structure your app. React lets you write lots of cheap and almost unbreakable lego pieces that you can assemble into larger blocks of functionality.
It is just a matter of time before someone goes "Why HTMX/Alpine ? Now we have this <replace with the latest new shiny thing>"
Point is that the JS ecosystem is always coming up with new stuff. And there is a use case for all of them, React included.
The issue is the ecosystem. There's a lot of complicated architectures out there which only apply in esoteric context. Another issue is with the tooling. More often than not, we have templates that advertises making things simpler, but instead promotes complex and complicated tools that you should not add to your codebase.
But more often than not, you only need to update the content of your web app, and do a few changes to the layout. For that you don't need a full blown frontend library. React shines when you want to do web applications, not websites. But they're all pretty much the same when it comes to the latter. htmx+alpine and the other similar solutions are better when you don't want to control the whole UI. But need to add interactivity to a few select place. Which is the majority of websites.
IMO it's important to discuss two related but separate things:
1) Why an interactive frontend at all? IMO this is the more important question, and basically boils down to "AJAX enables better UX since you're not refreshing the entire HTML on every page load". It's better (in theory) for end-users because actions and screen loads should be a lot quicker.
But that's only relevant when comparing to serverside frameworks (like old PHP, cgi-bin stuff, old government websites, etc.). Most modern frameworks have moved to this model, and even the old PHP stuff uses it piecemeal.
That answers the "why a frontend framework at all" question, but not "why React" specifically, when Alpine/Vue/Svelte/Anything Else can do a similar job.
2) My answer to that is simply "momentum", and that's because of history. React did what AngularJS (v1) tried to but could not: gained critical mass. For a while there, it was the only major option for teams that wanted to build an enterprise-grade frontend that could be easily shimmed into their existing backend, without the overhead and overcomplexity of Angular. React then was more of a UI library than an actual framework like Angular, and so you could jam in React anywhere you needed it, but leave it out where you didn't need it.
(These days that's of course changed with frameworks on top of React like Next.js, which adds a lot of features and preconfiguration for a one-click deploy.)
But I don't think those features are why people choose React for new projects. It's again the sheer momentum of React. It's here to stay, it's well-supported, it has a huge ecosystem, and – importantly – it's easy to hire and train for. It's the most popular standard for the time being.
You can hire any off-the-shelf React dev and they can start working in your recent-ish React codebase (especially if it has no older-style class components anymore) without much additional training, because they can take your existing state object and easily build a new component on top of it and make a workable screen. But it's not just that... it's that they can move to another team, and they're probably using React too. And when they switch companies, it's probably still React. It's a skillset that has a moderate learning curve (it's really not so bad if you spend a few days on it) but then can keep getting reused at many many places and career opportunities.
By contrast the other frameworks might have many of the same important features, but they're nowhere as easy to hire for, and there's no guarantee of their future popularity/sustainability. The JS frontend world moves really really fast, and there was a period a few years ago where entire frameworks and languages (CoffeeScript, Backbone, Knockout, Ember, and of course Angular) came and went, much hyped initially but then quickly fell out of fashion in 1-2 years.
By contrast, React and Next is very firmly entrenched now and probably will stay that way for a while; they've become the Wordpress/Drupal of the frontend world, the "good enough" default option that many teams will default to.
Of course individual teams can evaluate different frameworks and choose the one most suited to their needed complexity level, but if you don't want to think about it and/or want to maximize the chances that your codebase will still be maintainable by a new team a few years from now... it's probably gonna be React. Not because it's the best, but just because it has too much momentum to die in the short term.
At a certain scale (small-to-medium-sized businesses), it makes sense to just use a "good enough" option that's widely used across the industry, because it's easy to find help for. Whe...
React happens to be the most popular tool, and I find a lot to like about it.
htmx looks really cool to me and I probably would chose it if I were to build something like Hacker News with little interactivity where I just need to show a few things reactively, but it's just not the right tool for all applications.
Even if you build something simple and small you can use a small subset of React without any complex frameworks or libraries, just as a single library, and it will work fine and won't be too complex.
Disclaimer I have not used React extensively although I did work at my dayjob with Vue which is very similar.
For small, one-time projects? Use whatever makes you and your client happy. Just be aware it will be outdated in 5 years, so who will maintain it?
I know it. It is good enough. It is fast enough. DX is good enough although edges are prickly in functional/hooks. Massive 3rd party support. Does everything from simple to SPA that could make Slack work for example without a tech switch.
The downside to this is it makes code heavy sometimes. I use nextjs because I love the server side rendering and seo optimization it provides and use the client components (traditional react) to manage state.
Despite this - I can do frontend development as well - but I have reached a point where I cannot be asked to learn new Javascript framework or libraries. Personally I just think it has become bloated, so I leave that for those that do know/understand.. like full-time React developers. I cannot be bothered as I am sure some new toy will come out in the next few years and replaces it.
My opinion... and I understand it is apples and oranges... is web development could be much easier than it is today. Back in '00s we were using old school PHP or Classic ASP to return compiled HTML content and, mostly, full screen refreshes. Eventually, Javascript gained more responsibilities giving us JQuery. We had simple GET and POST functionalities, allowing us to refresh part of the page, etc.
By around the 2010's I remember KnockoutJS and, eventually, AngularJS. Web development was changing and I accepted this. While I saw some benefits with auto refreshing hooks or the like.. the end was a lot of bloat... more lines of code. Some of which is duplicated in the frontend as well as the backend.
Between 2016 to 2021 I stopped web development. I started a job as a GUI application developer. When I returned to web development I am now seeing Node being used... Grunt being used... with some websites sticking to JQuery while another is using React. I am in shock at how complicated things has gotten!
To me, with my years of experience, feel like I am going full circle back to the old methods of web development... just with modern tools! I leaned back to jQuery for personal projects. It is easy to follow (in my opinion) -- but I came across HTMX in 2023 and really enjoying it!
Using HTMX, With odd exceptions, my javascript code is mostly a compliment to my CSS. The amount of code is reduced by 60-80%!
Since then I have started using HTMX for web projects at work -- with positive feedback from the team. We are, one by one, moving our projects over to HTMX. It is just easier to deal with.
Our team only has half a dozen developers. We are mostly backend devs but OK with react. Some are better than others. We have agreed that with HTMX we are getting things done a lot easier. We no longer have to bother with the additional layer that is React. We also stopped using jQuery.
Now I am not suggesting HTMX replaces React for all cases. Its just, for our use cases, it has done the job remarkably well.. and we are doing complicated web sites.
Having a team of React devs may be needed in a number of scenarios - I, thinking of all websites I have built over the last 20 years, struggle to find one.
There is a saying that is going to offend, but I think those that dislike or hating on HTMX are those that have invested their time on (things like) React. I also hear that HTMX guys are like a cult. That might be true to a degree. I am sure some will suggest I am one of them with this comment.
- React creates jobs, but HTMX gets rid of them.
As always with new tech, I think htmx will create more frontend jobs after the fad is over.
However, a client that has a reliable connection has more options available to them compared to an unstable one.
If their internet connection is poor, then I would argue it is not just an htmx issue -- it is an overall website one! Does not matter whether fetching json or html. In this case we do not want all interactions to be server ones.
Now we are entering the territory of understanding what the client wants. In this case, it might be easier to build a GUI application or Android/IOS app, with a SQLite local database, queue requests, etc. So when they do have a connection, even momentarily, the queued data can get sent out.
Again - it depends on the problem!
That's a terrible premise to start with. No, websites back in 2007 didn't work well otherwise we wouldn't have changed so much since then.
> If their internet connection is poor, then I would argue it is not just an htmx issue -- it is an overall website one! Does not matter whether fetching json or html. In this case we do not want all interactions to be server ones.
In 2024 if your webapp can't open a settings page without internet connection it means your architecture is lame. There is no way requesting a server to open a modal is a smart strategy, period.
Depends what you mean by a Settings page. Generally speaking, when it comes to web applications... any requests need to come from the server... or changes a user makes needs to go back to the server. Period. It if doesn't... it doesn't. Spit a user friendly message.
I have webapps that need to update user requests there and then, so their changes are exposed on other media/platforms. So, for me, a "settings page that can be used without internet connection" is pointless for my projects.
Again - what you want from a Settings page may have merit in your projects but they are a waste of energy for mine. Again, as I stated earlier, it is all about the problem you are trying to solve. What is lame architecture to you is additional bloat in mine.
In a number of scenarios, using htmx might not be the right approach. I could also say the same thing for React.
I have nothing further to add to this. As you mentioned, there could be more work for frontend devs. I doubt it is because of htmx fad being over... more likely it is some "cool toy" choo-choo train that replaces React.
Also have a look at the frontend iceberg there's a lot more to frontend systems.