Overview
An expression can be an array of templates, so items.map(item => html`…`) works. That fits a short list that does not reorder.
repeat(items, keyFn, item => html`…`) keeps a DOM node with its key when the list inserts, removes, or reorders. In JSX that identity was key={…} on the element. Here it is the key function passed to repeat.
keyed(value, template) is the single-node version: when value changes, that subtree is thrown away and the template is shown fresh. Import repeat and keyed from mates.
.map() vs repeat() vs keyed()
|
API
|
Best for
|
Notes
|
|---|---|---|
items.map(…)
| Short / stable | Simple. Nodes may be recreated when the order changes. |
repeat(items, keyFn, tpl)
| Keyed lists | Moves existing nodes. Imported from mates. |
keyed(value, tpl)
| One subtree | Remounts when value changes. Imported from mates. |
Short list with .map()
A stable array of templates is enough when items do not move.
Keyed list with repeat()
repeat(items, keyFn, template) moves existing DOM nodes when the list changes.
keyed() remounts one subtree
When the key changes, the previous DOM for that expression is discarded and the template is shown fresh.