Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 38 ยท Projects and Practice

Project: Build a Todo List with Lit

Build a complete todo list in Lit with two cooperating components: add, toggle, delete and filter tasks, with keyed lists, custom events and accessible controls.

Time to combine what you have learned. In this project you build a todo list from two components: todo-item (one row) and todo-app (the list, the form and the filters). The child tells the parent what happened with events, and the parent owns the data. That is the same "properties down, events up" shape you will use in nearly every real Lit application.

You will practice reactive state, repeat with keys, custom events, conditional rendering and accessible form controls. Browser storage does not work inside the sandboxed preview, so persistence is shown read-only at the end.

Plan the data and the events

flowchart TD
    APP["todo-app<br/>owns: todos[], filter"]
    ITEM["todo-item<br/>props: text, done"]
    APP -- "text, done (properties)" --> ITEM
    ITEM -- "todo-toggle, todo-delete (events)" --> APP

State lives in one place. Children are display and input only.

The todos are an array of plain objects: { id, text, done }. Each object has a stable id, which we use as the key when rendering, so that toggling or deleting one row does not recreate the others.

Step 1: the item component

todo-item renders a checkbox and a delete button. It never changes the data itself: it announces the user's intent.

JavaScript
class TodoItem extends LitElement {
  static properties = { text: {}, done: { type: Boolean, reflect: true } };
  _toggle() {
    this.dispatchEvent(new CustomEvent("todo-toggle", { bubbles: true, composed: true }));
  }
  _delete() {
    this.dispatchEvent(new CustomEvent("todo-delete", { bubbles: true, composed: true }));
  }
}

Step 2: the whole app

Here is the complete program. Read the comments, then try it: add tasks, tick them, switch filters, and delete.

Todo list

How it works

  • Immutable updates. this.todos = this.todos.map(...) creates a new array. Lit compares property values with !==, so mutating the array in place (push) would not trigger an update. See reactive properties.
  • repeat with keys. Without keys, deleting the first row would reuse the first DOM node for the second item, which mixes up focus and checkbox state. Keyed rendering moves the right nodes.
  • Property bindings. .checked=${...} sets the DOM property, which is correct for checkboxes. An attribute binding would only set the initial state.
  • Events up. todo-toggle and todo-delete carry no data, because the parent already knows which id each row belongs to via the closure. See component communication.
  • Accessibility. The checkbox and delete button have labels that include the task text. The count uses aria-live="polite" so screen readers announce changes.

Step 3: persistence (read-only)

The preview is sandboxed, so localStorage is unavailable here. In a real page, add two small pieces:

JavaScript
connectedCallback() {
  super.connectedCallback();
  const saved = localStorage.getItem("todos");
  if (saved) this.todos = JSON.parse(saved);
}

updated(changed) {
  if (changed.has("todos")) localStorage.setItem("todos", JSON.stringify(this.todos));
}

updated runs after every render caused by a changed todos, so saving is automatic.

Try it yourself

  1. Add a "Clear completed" button that removes every done task.
  2. Make the text editable on double-click.
  3. Move the filter into the URL hash so a refresh keeps it.
  4. Extract the filtering and counting into a controller, as in reactive controllers.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the app replace the todos array instead of calling push?

    Reactive properties are compared with strict inequality. Mutating in place keeps the same reference, so no update runs.
  2. 2. What is the benefit of repeat with a key function?

    Keys let Lit move existing nodes instead of reusing them for different items, preserving state like focus and checkbox values.
  3. 3. How does todo-item tell the parent the user clicked Delete?

    Children announce intent with events; the parent owns the data and updates it.
  4. 4. Why bind .checked instead of the checked attribute?

    After a user interacts with a checkbox, the attribute no longer reflects its state. The property does.

Summary

  • One owner of state (todo-app), simple display children (todo-item).
  • Replace arrays and objects to trigger updates.
  • Use repeat with stable ids for dynamic lists.
  • Communicate upward with custom events; downward with properties.
  • Persist in updated, outside this sandbox.

Next: Project: Accessible Tabs Component