3 ms·
> I installed lit-html and got to work. “Components” were simply functions that returned lit-html template results. A big singleton at the top of the tree held
by azangru 6y ago
> I installed lit-html and got to work. “Components” were simply functions that returned lit-html template results. A big singleton at the top of the tree held onto all the application state, stored as a global variable within that module.
> The first hurdle came when a component needed local state. I could have lifted it to the singleton, but that would have broken the component’s encapsulation. I noticed that lit-html directives can keep state, so I decided to use them to build a tiny component library — ignoring a warning from the lit-html developers that this wasn’t a supported use case.
> My home-brewed library worked great… until I needed to run some code when a component appeared on the screen. I started digging through lit-html documentation and issues looking for a way to detect a directive’s lifecycle, but it became clear to me that going down that path would be painful.
So the author knows about lit-html, good. I guess the obvious question at this point is why didn't he continue down the lit path he was already on, and tried lit-element? Lit-element should have given him observable local state (it's class-based web components), and would have given him lifecycle hooks (e.g. connectedCallback for doing something when a component appears on the screen). Lit-element is from the same ecosystem as lit-html; yet the author doesn't seem to have explored it at all. Why?
- spankalee 6y agoYeah, that's disappointing. lit-html is only meant for templating, not components, and while you can kind of shoehorn components into directives. that's not ideal either. lit-html is entirely intended to be used with web components, which is can only create HTML and has no special support or syntax for components. As you note, LitElement is what you should use if you like lit-html and want components. We'll be making this more clear soon by bringing lit-html and LitElement closer together in documentation.