Lists and keys
Pick a stable key
Section titled “Pick a stable key”list(items, key, render, options?) tracks the array source. Use a stable identifier such as a record’s id for the key. Keys are strings or numbers and must be unique within the list. When a row moves, its key keeps the same DOM nodes and lifetime.
Type into a row’s note field and reverse the list to see the input follow the row.
Loading the example…
View source
import { list, mount, signal } from "min-reactive-framework";import type { Dispose } from "min-reactive-framework";import { button, div, input, li, span, ul } from "min-reactive-framework/html";import type { DemoLanguage } from "../example-language.js";
type Item = { id: number; title: string };
export function run(target: HTMLElement, lang: DemoLanguage): Dispose { const ru = lang === "ru"; const items = signal<readonly Item[]>([ { id: 1, title: ru ? "Первый" : "First" }, { id: 2, title: ru ? "Второй" : "Second" }, ]); let nextId = 3;
return mount(target, () => div( { class: "mr-demo-stack" }, div( { class: "mr-demo-line" }, button( { type: "button", on: { click: () => { items.value = [...items.value].reverse(); }, }, }, ru ? "Развернуть порядок" : "Reverse order", ), button( { type: "button", on: { click: () => { const id = nextId++; items.value = [...items.value, { id, title: (ru ? "Строка " : "Row ") + id }]; }, }, }, ru ? "Добавить строку" : "Add a row", ), ), ul( { class: "mr-demo-rows" }, list( items, (item) => item.id, (item, index) => li( { "data-row": item.value.id }, span(() => index.value + 1 + ". " + item.value.title), input({ "aria-label": (ru ? "Заметка для строки " : "Note for row ") + item.value.id, defaultValue: item.value.title, }), button( { type: "button", "aria-label": (ru ? "Удалить строку " : "Remove row ") + item.value.id, on: { click: () => { items.value = items.value.filter((entry) => entry.id !== item.value.id); }, }, }, ru ? "Удалить" : "Remove", ), ), { empty: () => li(ru ? "Список пуст." : "The list is empty.") }, ), ), ), );}Edit a value or the collection
Section titled “Edit a value or the collection”If you replace a record with a new object using the same key, its existing row receives the new record through item.value. index.value follows the row’s new position. Read either inside a binding when the display should react to it.
For an update independent of list traversal, keep the field in a separate signal:
import { list, mount, signal } from "min-reactive-framework";import { li, ul } from "min-reactive-framework/html";
const title = signal("First draft");const rows = signal([{ id: "a", title }]);
mount(document.querySelector("#app")!, () => ul( list( rows, (row) => row.id, (row) => li(() => row.value.title.value), ), ),);
title.value = "Published";In this example, the title write changes one text node. To add, remove, or reorder rows, assign a new array to rows.value. That triggers key reconciliation.
Empty lists and cleanup
Section titled “Empty lists and cleanup”Use { empty: () => li("Nothing here yet") } to render an empty state. A row can return one node or an array of children. Removing a key releases that row’s bindings and registered cleanup. Duplicate keys are checked before the DOM is patched.
See the DOM mutation example to inspect a single field update.