Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 38 ยท Projects and Practice

Lit Practice: Exercises and Common Mistakes

Learn the most common Lit and web component mistakes by running broken code and its fix: mutation, attributes vs properties, missing super calls, leaks, styling and events.

Everyone makes the same handful of mistakes when they start with Lit and web components. This lesson shows the broken version first, so you can see the symptom, and then the fix. Each example runs here. After the mistakes, a set of exercises lets you practice on your own.

Mistake 1: mutating instead of replacing

Lit decides whether to re-render by comparing the old and new value with !==. Pushing into an array keeps the same array, so nothing happens.

Mutation does not update

After the push, the text still shows only a: the data changed but the view did not. (Calling this.requestUpdate() also works, but replacing is cleaner.) Details are in reactive properties.

Mistake 2: attribute versus property

An HTML attribute is always a string. A boolean attribute is "true" whenever it is present, even if you write active="false". Objects and arrays should be set as properties.

Attributes are strings

To switch a boolean off in markup, remove the attribute; in code, set the property. See attributes and converters.

Mistake 3: forgetting super

If you override connectedCallback, disconnectedCallback, updated or firstUpdated, call the super version. Lit uses those hooks for its own work: without the call, the component may never render.

A missing super call

Mistake 4: listeners that never go away

A listener added on window or document keeps your component alive and keeps running after the component is removed. Remove it in disconnectedCallback. Better: write it as a reactive controller, so setup and cleanup live together.

JavaScript
connectedCallback() {
  super.connectedCallback();
  window.addEventListener("resize", this._onResize);
}
disconnectedCallback() {
  window.removeEventListener("resize", this._onResize);
  super.disconnectedCallback();
}
_onResize = () => this.requestUpdate();   // an arrow field keeps one stable reference

removeEventListener needs the same function you added, so an inline arrow passed to addEventListener can never be removed.

Mistake 5: styles that do not reach inside

Document CSS does not cross a shadow boundary. A page rule such as button { color: red; } will not style a button inside your component. The component must offer styling hooks on purpose:

  • CSS custom properties (--my-color) inherit through shadow boundaries.
  • ::part() exposes named elements (part="label") for outside styling.
  • ::slotted() styles slotted children from inside; see slots and composition.

Mistake 6: events that stop at the shadow edge

A CustomEvent without composed: true cannot leave the shadow tree. Without bubbles: true it does not reach ancestors. When outside listeners never fire, check both flags first. See dispatching events.

bubbles and composed

Only ok reaches the document.

Quick reference

Symptom Likely cause
View does not update Mutated an array or object in place
active="false" still on Boolean attribute is present
Component never renders Missing super.connectedCallback()
Memory grows after removing elements Listener or timer not cleaned up
Page CSS ignored Shadow DOM encapsulation: use custom properties or ::part
Outside listener silent Event lacks bubbles or composed
Form value missing on submit Not form-associated

Exercises

Try these without looking back. Each one mixes several ideas from the course.

  1. Counter with limits. Build step-counter with min, max and step number attributes. Disable the buttons at the limits and fire a value-change event.
  2. Toggle switch. Build an accessible ui-switch with role="switch" and aria-checked, toggled by Space and Enter, that works in a form.
  3. Accordion. Build ui-accordion with slotted headings and panels where only one panel opens at a time; follow the accessible widget patterns.
  4. Debounced search. Combine an input, a fake async search and cancellation, as in async data loading.
  5. Theme provider. Use the context protocol so a theme-provider passes a theme to nested children.
  6. Test it. Write five tests for any of the above with the testing lessons.

When each exercise works in a plain HTML page with no build step, you are ready to use Lit in production.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A component shows stale data after items.push(x). What is the fix?

    Lit detects changes by reference. Replacing the array changes the reference and schedules an update.
  2. 2. What does active="false" mean on a Boolean property?

    Boolean attributes are true whenever they exist. Remove the attribute, or set the property to false.
  3. 3. Why call super.connectedCallback() in an override?

    LitElement relies on its lifecycle hooks. Skipping the super call can prevent rendering and controllers from working.
  4. 4. A custom event is not heard outside the component. Which flags do you check?

    bubbles lets the event travel up the tree; composed lets it cross the shadow boundary.

Summary

  • Replace objects and arrays; do not mutate them.
  • Attributes are strings; use properties for rich data and booleans.
  • Always call super in lifecycle overrides and clean up in disconnectedCallback.
  • Shadow DOM encapsulates styles and events: use custom properties, parts, bubbles and composed deliberately.
  • Practice by building small components without a build step, then test them.

Next: Back to the Lit track overview