Sub topic 5
Rendering with the iterator directive
This chapter assumes you can write a basic LWC component, bind class properties with curly braces, use lwc:if, and call map on an array. It also assumes you know why for:each needs a key on the first element inside the loop.
What the iterator directive does
The iterator directive renders a list like for:each does. The difference is what each pass gives you. With for:each, you get the item. With iterator, you get a wrapper object with four properties:
| Property | Meaning |
|---|---|
| `value` | The current array element |
| `index` | The zero-based position of the element |
| `first` | `true` for the first element only |
| `last` | `true` for the last element only |
You choose the name of the wrapper after the colon. In iterator:it={contacts}, the wrapper is called it. You then read it.value, it.index, it.first and it.last.
A worked example
The JavaScript is the same as for for:each. The component 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 uses iterator and marks the first and last rows:
<!-- contactList.html -->
<template>
<ul>
<template iterator:it={contacts}>
<li key={it.value.Id}>
<span lwc:if={it.first}>First: </span>
{it.value.Name}
<span lwc:if={it.last}> (last)</span>
</li>
</template>
</ul>
</template>The output has three li elements. The first reads "First: Amy Taylor", the second reads "Ravi Patel", and the third reads "Lena Fischer (last)".
How the pieces fit together
Follow these points when you read the template:
iterator:it={contacts}goes on a<template>tag. It takes the place offor:eachandfor:item.- The
keygoes on the first real element inside the loop, here theli. It must be unique among siblings and stable between renders. Useit.value.Id, notit.index. it.firstandit.lastare Booleans. They work directly withlwc:if, so the template needs no extra JavaScript to mark the ends of the list.- Every property is read through the wrapper. Writing
{value.Name}fails becausevalueis not a name in scope. Write{it.value.Name}.
Where iterator helps
The directive is useful when the markup depends on the position of an item. Typical cases are:
- A header label or special style on the first item.
- A closing element or a missing separator after the last item.
- A breadcrumb trail where every item except the last is a link.
Here is a breadcrumb sketch. Only the last item is plain text.
<template>
<nav>
<template iterator:crumb={crumbs}>
<span key={crumb.value.Id}>
<a lwc:if={crumb.first} href={crumb.value.url}>{crumb.value.label}</a>
<span lwc:else>{crumb.value.label}</span>
</span>
</template>
</nav>
</template>Here the wrapper is named crumb, which shows that the name is your choice.
Common errors
- Putting the key on the
<template>tag. The key must be on the first element inside the loop, not on the<template>that carries the directive. - Using
it.indexas the key. The index changes when items are inserted, removed or reordered, so the key stops identifying the same item. Use a stable ID from the data. - Forgetting the wrapper name. Inside the loop,
{value.Name}and{first}do not resolve. Always prefix with the name you chose. - Expecting expressions. The template cannot evaluate
{it.index + 1}. Compute any such value in JavaScript before you render.
Exercise
A teammate writes <template iterator:row={items}><li key={row.index}>{value.Name}</li></template>. Name the two mistakes and write the corrected line, assuming each item has an Id.
Show answer
The key uses the index, which is not stable when the list changes, and the text reads value.Name without the wrapper name. The corrected line is <li key={row.value.Id}>{row.value.Name}</li>.