6 Lit library
This chapter covers
- How Lit simplifies creation of web components
- Lit decorators
- Lifecycle methods
- Event handling
- Sharing state between Lit web components
If you worked through chapter 3, you know that building vanilla web components is entirely doable. But you probably also noticed that it involves a fair amount of boilerplate code. That includes observing attributes, wiring up reflection between attributes and properties, and managing when a component should re-render. It all works, but after the third or fourth component you start thinking, "There has to be a better way." I’m happy to report that there is, and it’s called Lit.
Lit is a lightweight library from Google for implementing web components and is the successor to the earlier Polymer library. It keeps you close to the web platform you already know, while quietly handling the tedious parts for you. Think of Lit as a power tool for web components. It doesn’t change what you’re building, it just makes the building dramatically faster, often with about half the code of an equivalent vanilla web component. The code has clearer intent and fewer places for bugs to hide. Because Lit sticks so close to the standards, almost everything you learned in chapters 3 and 4 carries right over.