Sub topic 10
Rendering conditionals and lists
This chapter assumes you know how an LWC bundle is laid out and how {expression} binding reads class fields. Here you will use template directives to show or hide markup and to repeat it for each item in an array.
Why directives exist
An LWC template is not JavaScript. You cannot write {items.length > 0} or call a function inside braces. A template expression names a field or getter, and the class does the computing. Directives are special attributes that tell the compiler to include, skip, or repeat a block of markup.
Conditional rendering
Put lwc:if, lwc:elseif, and lwc:else on an element, usually a <template> tag so no extra DOM node appears. Each directive takes a field or getter, never an expression.
<!-- taskPanel.html -->
<template>
<template lwc:if={hasTasks}>
<p>You have {taskCount} tasks.</p>
</template>
<template lwc:else>
<p>Nothing to do.</p>
</template>
</template>// taskPanel.js
import { LightningElement } from 'lwc';
export default class TaskPanel extends LightningElement {
tasks = [];
get taskCount() {
return this.tasks.length;
}
get hasTasks() {
return this.tasks.length > 0;
}
}The getters hold the logic. When tasks is reassigned, the template re-evaluates them and swaps the blocks.
Older code uses if:true and if:false. They still work but have no else branch, so prefer lwc:if in new components.
Rendering lists
The for:each directive repeats a block for every array item. for:item names the loop variable.
<!-- todoList.html -->
<template>
<ul>
<template for:each={items} for:item="item">
<li key={item.id}>{item.label}</li>
</template>
</ul>
</template>The key attribute goes on the outermost element inside the loop, not on the <template> tag. LWC uses the key to match old and new elements when the array changes, so it can reuse DOM nodes instead of rebuilding them.
flowchart TDDo not use that fragment. The diagram below shows the decision flow.
Common errors
- Key from the index. Keys must be strings or numbers that are stable and unique among siblings. An array index changes when items move, which defeats the purpose.
- Missing key. The compiler rejects a
for:eachchild that has nokey. - Expressions in braces.
lwc:if={a && b}fails to compile. Make a getter. - Mutating in place. Pushing to a plain array field may not trigger a re-render of your own derived values. Reassign the field, for example
this.tasks = [...this.tasks, newTask].
When you need the position
iterator gives you the first and last flags while looping.
<template>
<ul>
<template iterator:it={items}>
<li key={it.value.id}>
{it.value.label}
<span lwc:if={it.last}> (last)</span>
</li>
</template>
</ul>
</template>Each it object has value, index, first, and last.
Exercise
Why does <li key={item.id}> work better than using the array index as the key when users can reorder the list?
Show answer
An id stays attached to the same item when it moves, so LWC can reuse and reorder the matching DOM element. An index changes for every moved item, so LWC would match the wrong elements and do extra work.