▲ | spankalee 17 hours ago | ||||||||||||||||||||||
> the fact that you can only pass strings as attributes 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. | |||||||||||||||||||||||
▲ | _heimdall 14 hours ago | parent | next [-] | ||||||||||||||||||||||
That approach passes values in JS rather than the DOM, right? I read the go comment as talking specifically about DOM attributes which can only be strings (well, you can have boolean attributes as well). Web components can be passed objects in JS, but its news to me if that is available in HTML. | |||||||||||||||||||||||
| |||||||||||||||||||||||
▲ | sporritt 11 hours ago | parent | prev [-] | ||||||||||||||||||||||
It is true that web components can have properties and accessors like any object. But what you cant do is pass anything other than a string to a web component's attributes in the markup. I wrote a short article about this when I was investigating web components with JsPlumb a while ago: 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> | |||||||||||||||||||||||
|