Sub topic 2
Rendering an array with for:each
This chapter assumes you know basic HTML and JavaScript, including arrays. It also assumes you know how an LWC component pairs an HTML template with a JavaScript class that extends LightningElement, and how to bind a class property with curly braces.
What for:each does
The for:each directive repeats a block of markup once for every element in an array. You put it on a <template> tag. The tag itself produces no output, so only the elements inside it reach the DOM.
The directive takes two attributes:
for:each={array}names the array to loop over. The value is a property or getter from the component class, bound with curly braces.for:item="name"names the variable that holds the current element on each pass. You write the name as a plain string, with no braces.
A first list
Start with the data. The class holds an array of plain objects, and each object has a unique Id.
// contactList.js
import { LightningElement } from 'lwc';
export default class ContactList extends LightningElement {
contacts = [
{ Id: '003A', Name: 'Amy Taylor' },
{ Id: '003B', Name: 'Ravi Patel' },
{ Id: '003C', Name: 'Lena Fischer' }
];
}The template loops over contacts and renders one <li> for each element.
<!-- contactList.html -->
<template>
<ul>
<template for:each={contacts} for:item="contact">
<li key={contact.Id}>{contact.Name}</li>
</template>
</ul>
</template>This renders three list items: Amy Taylor, Ravi Patel and Lena Fischer.
How the pieces fit
Inside the loop, contact refers to the current array element. You read its fields with dot notation, such as {contact.Name}. The name contact exists only inside the iteration template. Outside it, the template cannot see that variable.
The diagram below shows the flow for each pass.
The key attribute
Every iteration needs a key on the first element inside the <template for:each>. In the example, that is the <li>. The key must be unique among the siblings in that list, and it must stay the same for the same piece of data across renders. A record Id is a good choice. The next chapters cover why keys matter and how to diagnose key problems. For now, remember two rules:
- Put
keyon the first element inside the iteration template, not on the<template>tag. - Bind it with curly braces, as in
key={contact.Id}, so each item gets its own value.
Requirements for the array
The for:each value must be an array. Each element can be an object or a primitive, but objects are the common case because you usually need several fields per row. If the property is undefined or null, nothing renders and no error appears. This matters when data loads later from a server. Later chapters show how to display a message in that case.
Several elements per item
The iteration template can hold more than one element. Only the first one needs the key.
<template>
<template for:each={contacts} for:item="contact">
<div key={contact.Id} class="row">
<strong>{contact.Name}</strong>
<span>{contact.Id}</span>
</div>
</template>
</template>Here the <div> is the first element, so it carries the key. The <strong> and <span> inside it need none.
Common errors at this stage
| Symptom | Likely cause |
|---|---|
| Nothing renders | The array is empty, `undefined`, or the property name in the template does not match the class |
| Compile error about a missing key | The first element inside the iteration template has no `key` |
| Every row shows the same text | `for:item` name in the template differs from the name used in the braces |
| Quotes error on `for:item` | The name was written as `{contact}` instead of `"contact"` |
Exercise
In <template for:each={accounts} for:item="acct">, where does the key attribute go, and what should its value be if each account has an Id field?
Show answer
Put it on the first element inside the iteration template, such as the <li>, not on the <template> tag. Bind it to the unique identifier: key={acct.Id}.
Exercise
What happens when the array bound to for:each is undefined?
Show answer
Nothing renders and no error is shown. The loop simply produces no elements.