159 points · by tdumitrescu · 8y ago · 111 comments
One major issue we've had though, which wasn't touched in this article, is inter-component communication among siblings. Sure you can just do a global event bus, and that works, but I've yet to see an elegant solution for this yet.
You can do better than template literals. You can use TSX, which gives you compile-time verification for tags and attributes, in addition to JavaScript expressions.
See: https://github.com/wisercoder/uibuilder
With TSX (which is JSX written using TypeScript, you get things like syntax coloring and indentation for tags, because HTML and JavaScript are both first-class citizens in TSX.
React components are brittle, that's not FUD. For example, CSS classes used by React components can clash with other React components, or with the page that includes them. What's FUD about that?
It’s pretty flexible and thin.
[0]: https://vuejs.org/v2/guide/components-custom-events.html#syn...
Would Redux potentially be a good fit? It's a "global event bus" kind of thing, but with a pretty clear information flow model and an increasingly large user community. (Both of which are good things, IMHO.)
Redux feels like a step in the right direction conceptually with its centralized state bag, but I feel like it's a bit distracting (since it was primarily conceived to handle serializable actions for a very specific use case), and also doesn't have good patterns out of the box to organize side-effects (sagas kinda do, but people I've worked with are fairly divided on whether it works for them or not)
Recently I've been thinking that a lot of the complexity around React+Redux comes from wanting testable state, but as it turns out, Jest can mock imports making the entire context shenanigans somewhat pointless - just import a global state bag. I've had pretty decent success with just having a naive global state bag, would love to hear about similar approaches
Initially I was wondering why we didn't have a centralised store, but once free from that mindset it allows you to keep observable data locally to where it's needed. Much nicer for encapsulation and separation of concerns.
Far less boilerplate too, as MobX does some very smart usage & dirty checking for you.
mobx-react also provides an @observer class annotation, which ends up working pretty magically - things re-render when they need to.
If anyone's interested, here are some tutorials from the author: https://egghead.io/courses/manage-complex-state-in-react-app...
In a discreet component architecture there is basically no reason for arbitrary siblings to know anything about each other - if there is common state to be shared it should be inhereted.
Granted, in JS land that sometimes involves jumping through weird IoC hoops or cached callbacks, which are akward to say the least, but in the abstract an instance of a component should be concerned with its own behaviour, and possibly that of it's descendents by way of that behavior.
The way we typically handle something like this is by connecting components through a common ancestor. The default naive behavior of Panel apps is for linked components to share a "centralized state bag" (as one of the other comments calls it re: Redux), and updates to that state automatically cascade to all the components. It's a blunt, easy and lightweight way to propagate changes arbitrarily throughout an app, but it does get unwieldy as the state object gets big so we've ended up with some mechanisms for partial state-sharing to address this issue.
Tossing in Redux (or whatever Flux-like flavor of the day) is a low-effort, high-value add on top of React and obviates the possibility of some bat-shit data-passing situation that would make an OO inheritance junkie blush.
If you're the sensitive to boilerplate type, there are some simple libraries to solve that issue. Unless you're tossing together a three component TODO list demo, not using Redux will just bite you in the ass a year down the line in maintenance costs as your application grows.
This is really starting to remind me of the "Don't worry about the data model, just toss it in Mongo!" all over again.
Most communication I solve by custom event dispatching or attribute callbacks, but there are the odd cases, especially regarding more advanced drag/drop, that still requires state at some level. Do you have any concrete example of inter-component communication to look at?
Are you using some template tag function that handles escaping? Seems risky to rely on manual escaping everywhere.
Having a component tree (because the rendering model of a browser requires it) does not mean state management needs to be cramped into the same tree or use a tree in the first place!
A DOM tree itself doesn't do any kind of binding and it doesn't do inter-component communication. (it merely offers events) Why should web components? Keep them as dumb as DOM nodes please.
A lone H1 element doesn't register itself on some event bus, doesn't observe attribute changes and doesn't create odd 'actions' tied to some obscure state management system. And it shouldn't.
Zone js implements zones and by that they apparently mean exactly 1 zone.. making a supposed abstraction where you can't safely add 2 implementations of web components (or apps?) to 1 page without planning for that with 1 global setup for them..
My initial impression of shadow DOM is that I would get the same scope separation as iframes with a nicer method of passing data and describing the layout.. Then I assumed web components were this missing piece finally implemented.. now I'm wondering if I completely misjudged and everyone else really wanted a complicated footgun where seeming builtins are colliding with your namespace?
The result is your can never just use the basic DOM APIs and devtools to inspect and interact with your application's components. Instead, you always have to get at them through some other API, and understand how the UI and state you see in the DOM is produced and updated by some mediating layer (virtual DOM, view hierarchy, etc.). It feels like trying to get at the truth by talking with two different people, who each only possess a subset of the facts.
In reality, browser engineers are not that stupid. Mutating the DOM will queue a layout operation, but it will not actually occur until the current JS task has finished executing. Within a JS task, such as an event handler, an XHR completion, or a setTimeout, you can mutate the DOM as many times as you like, and it will only result in a single layout pass.
(The exception is if you try to read back some measurement from the DOM, such as an element's bounding box, after mutating it. In this case, the browser does have to block while it performs layout, but that is not something can be solved with virtual DOM).
So, what is the purpose of the virtual DOM? It was invented to allow React to provide the illusion of full re-rendering. React's authors wanted to a provide an experience similar to that found on the server, where an entire HTML page is re-rendered from scratch for every load. In that way, there is never any possibility of part of the HTML becoming stale, as it all gets recreated from scratch each time.
However, the browser DOM is not designed to be blown away and recreated from scratch all the time. Nodes are expensive objects, spanning the JS and C++ worlds. Recreating the whole DOM tree each time any part of it needed updating would be too slow. So, instead, they created the virtual DOM as an intermediate data structure. React renders the virtual DOM. The virtual DOM is diffed against its previous state, and then the changes are applied to the actual DOM tree. In that way, every component's render() method can be executed, but only those parts of the DOM that have actually changed will be updated.
It's a nifty optimisation, but it's not about avoiding reflow, it's just another method of dirty checking, similar to that done in other frameworks like Angular or Ember. It's just that React chooses to diff the data structure produced by render(), rather than diffing the model data that is later used for rendering.
It goes into some details how different approaches in different frameworks work, and why. Moreover, it shows how you basically need to re-implement Virtual DOM and other tricks in vanilla JS code to approach the same speed.
Yes, browser engineers are not that stupid. But they don't batch operations as efficiently as a proper Virtual DOM implementation would (including efficiently handling event listeners, looking for clues like `keys` on repeating DOM elements etc. etc.).
The draw for me is that I don't need to manage the state of a bunch of DOM objects anymore. I wouldn't even mind if React were slower- the performance isn't really the point.
I don't and will never want web components for this reason.
There's also a lot of "implicit state" not captured in such a virtual DOM, like the user's current selection, the scroll position, form values, and so on. If these are blown away, it creates a really poor experience. The virtual DOM isn't just an optimization, it is actually a correctness fix.
Perhaps there should be. Has there been any discussion on standardizing such a thing?
The only real missing piece is the concept of props over attributes. If I want to pass an array of ids to a sub-component declaratively, there's not really an easy way to do it. Hopefully there will be some consensus about this soon. The best thing is that this is all part of modern browsers without libraries, or even precompilation. Not every web app needs massive libraries, so having web components as a built-in, light-weight component system is fantastic.
Plenty of HTML templating languages allow for setting properties declaratively. Polymer, lit-html, Vue, Angular, all do.
For instance, in lit-extended (an extension of core lit-html), properties are set by default, attributes have a '$' suffix, and event handlers have an 'on-' prefix:
html`
<my-element
aProp=${foo}
an-attribute$="${}"
on-click=${clickHandler}>
</my-element>
`;I'm not sure that HTML itself will get a standard way to set properties, even with the Template Instantiation proposal. Encoding the intent to set a property is hard given that all valid attribute names are already valid attribute names, and could have current semantics. Adding new support for currently invalid attribute names sounds like a very difficult thing to do.
This can stay at the level of opinionated libraries. You learn how to use that library and it's method for setting properties, and you don't have to worry about other components choices for templating.
This seems like a framework layer feature and not something that should be baked into the standard. I don't think web components are supposed to act like a framework and optimize a particular workflow or programming style.
I think the way snabbdom implements props just assigns Javascript properties to the DOM object.
Yes, there is. See https://github.com/wisercoder/uibuilder
Polymer has always been a set of helpers for making Web Components. There are others like SkateJS and Stencil.
Vue is not normally Web Components, but they have an option in CLI 3 to compile to Web Components.
React and React native have basically nothing to do with Web Components, though React can use Web Components because Web Components are just HTML elements.
Yup. And you can't use Polymer elements in React because React passes data as attributes whereas Polymer expects properties.
And then there's the issue of event handlers etc. etc. etc.
"Web Components" are now a largely non-intersecting collection of mutually incompatible frameworks and libs: https://custom-elements-everywhere.com
Good luck using Polymer's <custom-element binding="{{a.b}}"> anywhere
This is mostly React's existing opinion that elements don't even need properties set, and is not the fault of Polymer or Web Components. Most Polymer elements are fine with simple types like strings or numbers being set as properties or attributes.
> "Web Components" are now a largely non-intersecting collection of mutually incompatible frameworks and libs: https://custom-elements-everywhere.com
That is a site that says how well _frameworks_ work with Custom Elements, ie do they make closed-world assumptions about HTML that interfere with using Custom Elements. It says nothing much about what a Web Component is, so I'm not sure how Web Components could be "non-intersecting".
> Good luck using Polymer's <custom-element binding="{{a.b}}"> anywhere
I know you troll every Web Components and Polymer discussion you can now, but this is just a non-sequitur. Polymer's template syntax is only used to implement elements with Polymer. `<custom-element binding="{{a.b}}">` would only ever occur in a Polymer template and is not intended to be used "everywhere", so no luck is needed, thank you.
Not to be harse. But for a company as big as Mixpanel, the blog UI is terrible. No branding at all and seems unprofessional.
Even more ironic is they say this as the first line in the post: "Clean, attractive user interfaces and effective user experience have always been pillars of Mixpanel’s products."
They should look at companies like GitHub, Facebook, Twitter, and the likes for an example.
Check the Mixpanel blog (https://mixpanel.com/blog/) and you'll find it the brand consistency more similar to the other tech companies you mentioned.
I'm actually okay with engineers spending less time on shiny css and more time on the content of their posts.
Maybe it was due to how simple the example was, but having to click back many times based on how many times a user used the interface is just bad design.
I'm looking forward to a future where we use web components.
How is this "making web components work" any different from making DOM work via snabbdom or React?
or is it strictly a web-browser-only thing?
Check out https://github.com/Polymer/pwa-starter-kit/wiki for concrete examples using LitElement (https://github.com/Polymer/lit-element)
Edit: looks like you've been doing this a lot, and it's really not what the site is for. Would you mind reading https://news.ycombinator.com/newsguidelines.html and using HN as intended? The idea is: if you have a substantive point to make, make it thoughtfully; if you don't, please don't comment until you do.
It's not Web Components that are happening, it's the dozens of frameworks required to make them work that are happening.
Even mixpanel's library "does so by providing an easy-to-use state management and rendering layer built on Virtual DOM (the basis of the core rendering technology of React). Through use of the Snabbdom Virtual DOM library and first-class support for multiple templating formats" [2]
Web Components are a very low-level extremely limited crummy DOM-based API that no one in their right mind wants to use. They are not happening.
---
[1] https://fronteers.nl/congres/2011/sessions/web-components-an...