For
Component for rendering lists that automatically updates when the array changes.
Signature
function For<T>(
items: () => Array<T>,
renderItem: (item: T, index: number) => Node
): Node;Parameters
Returns
Example
const [tasks, setTasks] = createSignal([
{ id: "I", text: "Inhale", completed: true },
{ id: "E", text: "Exhale", completed: true },
{ id: "R", text: "Repeat", completed: false },
]);
const template = html`
<div>
<h2>Todo List</h2>
<ul>
${For(
tasks,
(task, index) => html`
<li>
<input
type="checkbox"
checked="${task.completed}"
onchange="${() => console.log("toggleTask", task.id)}"
/>
<span>${index + 1}. ${task.text}</span>
<button onclick="${() => console.log("delTask", task.id)}">
X
</button>
</li>
`
)}
</ul>
</div>
`;Advanced Usage
Behavior
Performance Notes
Last updated