Render a collection

rows is reactive state containing a vector. When it changes, the keyed loop updates the displayed rows automatically.

counter.wisp
<script>let rows: Signal<Vec<u32>> = Signal::new(vec![1, 2, 3]);let next_id: Signal<u32> = Signal::new(4);let add = move || {    let id = next_id.get();    rows.update(|items| items.push(id));    next_id.set(id + 1);};let reverse = move || rows.update(|items| items.reverse());let remove = move || rows.update(|items| { items.pop(); });</script><style>:global(body) { display: flex; flex-direction: column; align-items: flex-start; gap: 12px; padding: 16px; }button { padding: 8px 12px; }</style><button onclick={add}>Add</button><button onclick={reverse}>Reverse</button><button onclick={remove}>Remove last</button><div>  {for row in rows.get() key=row}    <div>{row}</div>  {/for}</div>
vapors component source

Save this as crates/demos/demos/counter.wisp, then run cargo run -p vapors-app --example demo -- counter.

Press Add to append a row, Reverse to reorder the rows, and Remove last to remove the final row. Remove all the rows, then add one again. The number assigned to each new row is its identity and is not reused.

Choose a stable, unique key

The integer value identifies each row here. For application records, use an immutable ID rather than an editable label. A retained key preserves its row’s nodes while item and index signals update in place.

Without an explicit key, the index identifies the row. That identifies positions rather than records when items move.

Duplicate keys are refused

The runtime validates the next keys before changing the rows. Duplicates cause a refusal naming the key and source position; the previous rows remain. A later valid update can proceed.

Template loops and HTML lists are separate

{for} works with supported containers such as the div elements here. HTML ul, ol and li are not rendered by this revision. This limitation does not prevent rendering collections.

The scroll demo uses keyed rows in two independent scroll containers.