Step 1 — App Shell and Reactive State

In this first step (~10 minutes), you build the root TodoApp shell with three seeded tasks, reactive filter buttons (All, Active, Done), and a live counter showing how many tasks remain. There are no input forms or subcomponents yet — the goal is to wire reactive state and keyed list rendering cleanly.

The live interactive demo and complete source for this step appear at the bottom of the page.

Defining task state with use_reactive_list() and Signal#

Each task stores an integer id, a title, and a reactive done cell (Signal[bool]) so individual completion flags can update later without replacing the whole list:

from typing import TypedDict

from webcompy.signal import Signal, use_reactive_list, use_state


class TaskItem(TypedDict):
    id: int
    title: str
    done: Signal[bool]

Inside TodoApp, initialize both tasks and filter_mode with factory callables (lambda: ...) so setup stays friendly to pre-rendering and hydration:

tasks = use_reactive_list(
    lambda: [
        TaskItem(id=1, title="Read Getting Started", done=Signal(True)),
        TaskItem(id=2, title="Build TodoApp state", done=Signal(False)),
        TaskItem(id=3, title="Split into components", done=Signal(False)),
    ]
)
filter_mode = use_state(lambda: "all")

Deriving counts and filtered views with use_computed()#

Derived values compute automatically whenever tasks.value, any task's done.value, or filter_mode.value changes:

from webcompy.signal import use_computed

remaining = use_computed(lambda: sum(1 for t in tasks.value if not t["done"].value))
visible_tasks = use_computed(
    lambda: [
        t
        for t in tasks.value
        if filter_mode.value == "all"
        or (filter_mode.value == "active" and not t["done"].value)
        or (filter_mode.value == "done" and t["done"].value)
    ]
)

Because use_computed tracks every signal read during execution, you never have to subscribe or recalculate counts manually when switching filters or toggling tasks.

Rendering keyed lists with repeat() and filter buttons#

Pass visible_tasks to repeat(..., key=lambda task: task["id"]) inside html.UL(...). Supplying a stable key= lets WebComPy reuse existing <li> nodes across filter switches:

from webcompy.elements import html, repeat

html.UL(
    {"class": "task-list"},
    repeat(
        visible_tasks,
        lambda task, _key: html.LI(
            {"class": use_computed(lambda: "todo-item done" if task["done"].value else "todo-item")},
            task["title"],
        ),
        key=lambda task: task["id"],
    ),
)

Each filter button updates filter_mode.value on @click and highlights the selected button using a computed "class" attribute. Attach CSS rules to TodoApp.scoped_style using WebComPy's semantic design tokens (var(--color-accent), var(--space-2)).

Next steps#

Step 1 Live Demo

Demo
Code
from typing import TypedDict

from webcompy.app import WebComPyApp
from webcompy.components import ComponentContext, define_component
from webcompy.elements import html, repeat
from webcompy.signal import Signal, use_computed, use_reactive_list, use_state


class TaskItem(TypedDict):
    id: int
    title: str
    done: Signal[bool]


@define_component()
def TodoApp(_: ComponentContext[None]):
    tasks = use_reactive_list(
        lambda: [
            TaskItem(id=1, title="Read Getting Started", done=Signal(True)),
            TaskItem(id=2, title="Build TodoApp state", done=Signal(False)),
            TaskItem(id=3, title="Split into components", done=Signal(False)),
        ]
    )
    filter_mode = use_state(lambda: "all")

    remaining = use_computed(lambda: sum(1 for t in tasks.value if not t["done"].value))
    visible_tasks = use_computed(
        lambda: [
            t
            for t in tasks.value
            if filter_mode.value == "all"
            or (filter_mode.value == "active" and not t["done"].value)
            or (filter_mode.value == "done" and t["done"].value)
        ]
    )

    def filter_button(label: str, mode: str):
        return html.BUTTON(
            {
                "type": "button",
                "class": use_computed(lambda: "active" if filter_mode.value == mode else ""),
                "@click": lambda _ev: setattr(filter_mode, "value", mode),
            },
            label,
        )

    return html.DIV(
        {"class": "todo-shell"},
        html.H1({}, "ToDo Tutorial"),
        html.DIV(
            {"class": "toolbar"},
            html.SPAN({"class": "remaining"}, use_computed(lambda: f"{remaining.value} remaining")),
            html.DIV(
                {"class": "filters"},
                filter_button("All", "all"),
                filter_button("Active", "active"),
                filter_button("Done", "done"),
            ),
        ),
        html.UL(
            {"class": "task-list"},
            repeat(
                visible_tasks,
                lambda task, _key: html.LI(
                    {"class": use_computed(lambda: "todo-item done" if task["done"].value else "todo-item")},
                    task["title"],
                ),
                key=lambda task: task["id"],
            ),
        ),
    )


TodoApp.scoped_style = {
    ".todo-shell": {
        "max-width": "32rem",
        "padding": "var(--space-3)",
        "font-family": "var(--font-sans)",
        "color": "var(--color-fg)",
    },
    ".toolbar": {
        "display": "flex",
        "justify-content": "space-between",
        "align-items": "center",
        "gap": "var(--space-2)",
        "margin": "var(--space-3) 0",
    },
    ".filters": {"display": "flex", "gap": "var(--space-2)"},
    ".filters button": {
        "padding": "var(--space-1) var(--space-3)",
        "border": "1px solid var(--color-border)",
        "border-radius": "var(--radius-sm)",
        "background": "var(--color-bg-card)",
        "color": "var(--color-fg)",
        "cursor": "pointer",
    },
    ".filters button.active": {
        "background": "var(--color-accent)",
        "border-color": "var(--color-accent)",
        "color": "var(--color-on-accent)",
        "font-weight": "600",
    },
    ".task-list": {"list-style": "none", "padding": "0", "margin": "0"},
    ".todo-item": {
        "padding": "var(--space-2) var(--space-3)",
        "border": "1px solid var(--color-border)",
        "border-radius": "var(--radius-sm)",
        "background": "var(--color-bg-card)",
        "color": "var(--color-fg)",
        "margin-bottom": "var(--space-2)",
    },
    ".todo-item.done": {"text-decoration": "line-through", "color": "var(--color-fg-muted)"},
}

app = WebComPyApp(root_component=TodoApp)
app.run()