by spankalee · 1y ago · 0 comments
While a lot of people view web components as competitors to frameworks, they don't really have to be. The just define an interface between component implementations and browsers so enable interop and reliable composition.
On top of the low-level APIs frameworks have a lot of room to innovate and customize:
- There is a huge range of possibilities an opinions on how to author components. Buildless, JSX, template literals, custom syntaxes and compilers, class-based, functional, etc.
- There is a lot room for gluing components together in different ways: custom component loaders, context protocols, SSR, suspense-like utilities, styling and theming frameworks, etc.
- State management cuts across the UI independently from components and has a lot of room for innovation.
Being able to say "Use our new Flugle framework, it works great with all the other frameworks and adds awesome scaffolding" should be a nice selling point and buffer against React monoculture, as opposed to building a different and much smaller silo.
For instance, I have a project with a small web component that displays data from a duckdb wasm connection. The connection object is passed as a property and it works flawlessly.
Attributes are the default for html
"This folder contains React components. Create a new folder `components-vue`, and translate every component from React to Vue/Solid/Vanilla"
ymmv
My main problem with web components that you end up with a build step anyway.
Not every component is interactive and relies on JS, some are just static elements (say avatar image) that you wanna render right away (but at the same time keep styles and logic scoped). Declarative shadow DOM helps, but you end up with a bunch of duplicate code in your final output, which begs the question - why am I using web components at all.
But it turns out that those layers of indirection have made the web platform just another build target. It's just like how they never added optional types to JavaScript and now TypeScript is the de-facto standard.
And now I'm stuck managing layers of indirection and compiler settings and debugging in nearly the same but /slightly/ different code than what I programmed in.
Their logic is that if it can be done using a compiler or on the server side, then why bother doing it in the platform? Which is partially true: I want a compile step to optimize everything. But there is room in-between and this is often just an excuse to ignore dev UX entirely.
I've been using web components using a wrapper to avoid all boilerplate. It gets me 80% of web component functionality with very little effort: https://github.com/lelanthran/ZjsComponent
Discussed on HN previously: https://news.ycombinator.com/item?id=44290315
Now this is not perfect, but there is, for me, no easier way to create and use new html components.
And then we are not actually far from what we were always able to do with traditional templating engines, which already allow us to define separate reusable components. It is what they were made for. Full circle back, throw out all the JS frameworks.
For example, a guy I know online who is an argumentative, boring backend dev that regularly has really bad and uninformed takes on things he has very limited experience with, he recently said he prefers web components. For all intents and purposes, he had ~0 web development experience.
About once a year I give web components another shot and rediscover all the things I hate about them. They are not designed for what web developers consider components and they are full of footguns and bizarre limitations.
Sometimes you will hear this subtly acknowledged as “web components excel at leaf nodes”. What this means in practice is that you shouldn’t use them for things that have contents. They are fine for their original use case, form elements where you don’t want page styles to affect things like the drop-down arrow in the control. But they are a massive disappointment for the typical use case.
I avoid frameworks like a plague. Plain JS, web components and some good domain specific libs are more than enough to cover all my needs
For many use cases, there's not much difference between writing a Web component or an IIFE (Immediately Invoked Function Expression) that attaches your JS functionality to a div somewhere like we wrote JS without jQuery 15 years ago, although Web components are more elegant. But still, they are mostly packaging.
I say this as someone who likes Web components and who created several ones that are used in production. But just yesterday, when I added a new attribute to one of my Web components and wondered for a moment why the new attribute wouldn't get rendered, I realized that I forgot to add the code that imperatively tells the DOM how to update itself. Which is okay, because this is just a small component and it's pretty lightweight. I'd never use a framework for stuff that can be achieved without much effort in vanilla JS.
My point is: selling Web components as a way out of this trap is disingenuous. They don't offer anything at all that is important for modern frontend dev.
Being able to use a different library with a component, instead of the component being tied to React, is the whole point.
React isn't 100x more popular because its reactivity system or any other feature is 100x better. Half the reason it's popular is network effects – too many frontend components / libraries are made React-only even though they don't need to be React-specific.
Those network effects are the trap, not the reactivity system that's as good as any other for the purpose of writing a Web Component. If you don't want to use simple and small tools like Lit.js, that's fine, but that's your choice, not a limitation of Web Components.
The point of Web Components is not to provide a blessed state management or virtual DOM implementation that will have to stay in JS stdlib forever, it's to make the components you author compatible with most / all UI libraries. For that goal, I don't know of a better solution.
However, out of the box, Web components don't come with almost anything. Comparing React to Web components is comparing apples to oranges.
Lit is great, but Lit is a framework. Now you're comparing React with Lit. Different story than React vs. vanilla Web components.
You could take a Lit-based web components a rip Lit out and you would still have the same component that you can still use anywhere. Lit is just an implementation detail.
I mean, yes, but you're the one making this comparison, saying that WCs lack reactivity etc.
Web Components are an extension of the DOM – a low level browser API. They are similarly low level. That's expected. I don't need or expect them to be something more.
I am happy that I can use any reactivity system I want to implement a Web Component. That's a feature, not a bug. Having implemented a reactivity system myself, I know that there isn't a perfect one, the design is full of tradeoffs, and I'd rather not have a blessed implementation in the browser, because it will inevitably turn out to be flawed, yet we won't be able to retire it because "we can't break the web". A blessed implementation like that would benefit from network effects just like React does, and would have all the same problems as React, plus the inability to rapidly innovate due to the browser's unique backwards compatibility concerns. I'd rather ship an extra 3KB and avoid all those problems.
This is especially difficult when you start running into complicated issues like render props in JSX-based frameworks, context for passing state deeply into a component, or slots/children that mean reactivity needs to be threaded through different frameworks.
What they offer is interoperability and the ability to use components outside of a framework or with a framework.
And that's why they help solve the ecosystem trap.
Furthermore, Web Components enforce good patterns; like the fact that you can only pass strings as attributes (by-value) is actually genius as it encourages simple, minimalist component interfaces and avoids pass-by-reference issues for which React community had to invent an entirely new paradigm to protect against (I.e. Redux state management with functional programming approach).
And the great irony is that a lot of the top people who are still pushing React are basically rich from Facebook shares and don't have to work anymore. In effect many of them are forcing this technology onto young people who have no choice in the matter whilst they themselves don't need to use it or do any coding anymore. Meanwhile I know a lot of developers who want to leave (or have left) the industry because of how bad it is and how few decisions they're allowed to make.
It's demoralizing to work with inferior tools when you know better tools exist because you use them in side projects... When you see this, you think to yourself "If the company forces me to be inefficient with my choice of tooling, this gives me a license to be inefficient in other ways."
Personally, I don't even code anymore (only on my side projects). It's a shame because one of my main talents is writing clean, minimalist code. In my day job, I'm using drag-and-drop UI platforms like n8n and Flowise (for AI). It's refreshing to be able to use vanilla JS inside the nodes, without a compile step and on a real-world project that actually pays. These UI platforms are actually much more predictable to work with than React. When I was using React (for almost a decade), I was constantly debugging weird glitches and state management issues; I never encountered those with Web Components or with platforms like n8n.
This isn't true at all though. It's a lie started in the early days by React engineers that just won't die, unfortunately.
Web components are objects and they can have properties and accessors like any object. The vast majority of declarative template systems like React, Lit, Vue, Angular, Svelte, Solid, etc., will declaratively set properties - which can carry any type of JavaScript value, including complex objects and function - on web components which can then be used to update the component's DOM.
Web components can be passed objects in JS, but its news to me if that is available in HTML.
Simple interfaces are critical for good architecture. Forcing everything to be a string, forces pass-by-value and forces minimal communication between components. You get maximum separation of concerns; high cohesion, loose coupling.
I know exactly what inputs each component is dealing with by just looking at the HTML. This is incredibly useful... And components can output stuff by dispatching events on themselves (with bubbling for important events which may potentially affect higher level components).
this.someProperty = JSON.parse(this.getAttribute('some-attribute'))https://jsplumbtoolkit.com/blog/2024/07/18/wrapping-data-in-...
TL;DR I ended up creating a context object and a BaseComponent that can access the context. Subclasses of the base component prefix their attributes with a colon to instruct the code to look in the context:
<date-label :value="now"></date-label>
Also, I tend to think of HTML not as my application view, but as a document that represents a serialization of your view. The actual, live view itself is the DOM. Once that document is parsed and rendered, the HTML source from whence it came doesn't matter anymore. Stringly attributes should no longer be a concern.
Though, admittedly, the HTMLTimeElement's dateTime property is still a string value. I imagine that is more of a legacy issue. The Date class in JavaScript is a mess as well.
I promise writing applications for the browsers is not challenging. You don’t need big frameworks or component madness that’s more of the same.
You don't, but in any sufficiently complex app you'll end up writing a sort of 'mini framework' of your own as you abstract all the things that crop up more than a few times. That framework might be really nice at the start but it'll get more and more hacky as the project continues, especially if you're constrained by resources. Eventually you'll regret not using something standard.
If there are more than a couple of developers on the project it'll be something no one really likes after a year or two.
If there are more junior developers it'll be a reason for them to want to get off the team because they won't want to be a part of the 'old legacy code'. Then it'll be hard to find people who want to join.
Eventually, as it gets harder to recruit people to the team because it's on a weird, legacy framework that no one knows, there'll be a big project to refactor it on to something more standard. That'll probably be React. At the same time most of the senior developers will be calling to scrap the codebase entirely and rebuild it (wrongly in almost every case, but they don't care and want a greenfield project to play with new things on.)
This is a story that has played out at every large org that builds apps internally, and probably a lot of startups as they mature and need to hire more devs. You might as well skip all of it and use a standard framework from the start.
Anyways this idea of internal frameworks has always been weird to me. Nobody says this of code outside the browser, so why would they say this inside the browser? When I think about in those terms this clearly becomes a simple organizational problem and I don’t need a framework telling me how to organize things like a parent telling me to do chores.
If you want reactivity in web components, you need a wrapper or another framework/small library.
Yeah it is. I don’t like React but I’ve been doing this since the days of MooTools all the way through Backbone to the libraries we have today. Once your app reaches a certain size and/or reaches a critical mass of contributors it does get challenging and modern frameworks reduce that challenge.
That can be taken way too far. Almost every time I’ve worked with Redux I’ve found it infuriatingly unnecessary. And React is far from the best framework out there. But it is a huge benefit all the same.
With regard to application size we fortunately now have TypeScript. All you really need to scale any application is types, functions, and code reuse.
It just takes the minimal amount of discipline, and some conventions. I know it can be done because I did it, lots of people did it just a few years ago (some of those apps are STILL running today.)
Web components are fantastic. They are the real future.
Aren't web components a pain in the ass to use?
And they come with extra benefits like no build tool required and native browser support.
Are you talking about functional components instead of class components? What big changes am I missing here? It seems pretty static to me.