Sub topic 8
Showing a message when the list is empty
A list template that renders nothing leaves the user guessing. Did the data fail to load, or is there simply nothing to show? This chapter adds a fallback message that appears when the array is empty.
This chapter assumes you know the for:each directive, the key attribute, and basic lwc:if and lwc:else conditional rendering.
Why the template needs a getter
A template can read a property, but it cannot run an expression. Writing lwc:if={contacts.length > 0} is a compile error because the comparison is not allowed. The standard fix is a getter in the component class that returns a boolean. The template then binds to the getter like any other property.
The pattern
Put the list and the fallback side by side, and let lwc:if and lwc:else choose between them. Only one of the two is rendered at a time.
<!-- contactList.html -->
<template>
<ul lwc:if={hasContacts}>
<template for:each={contacts} for:item="contact">
<li key={contact.Id}>{contact.Name}</li>
</template>
</ul>
<p lwc:else>No contacts to show.</p>
</template>// contactList.js
import { LightningElement } from 'lwc';
export default class ContactList extends LightningElement {
contacts = [];
get hasContacts() {
return Array.isArray(this.contacts) && this.contacts.length > 0;
}
addContact() {
this.contacts = [
...this.contacts,
{ Id: '003A', Name: 'Amy Taylor' }
];
}
}The getter checks Array.isArray first. Data that arrives from a server call or a parent component can be undefined or null before it loads. Reading .length on either one throws an error, and the guard avoids that.
Because hasContacts reads this.contacts, the template re-evaluates it whenever contacts is reassigned. Calling addContact replaces the array, the getter returns true, and the <ul> replaces the <p>.
Where to put the condition
Place lwc:if on the wrapper element, here the <ul>, and not inside the iteration. If you put it on the <li>, the browser would still get an empty <ul> when there are no items. An empty <ul> is invalid for some assistive technologies and can leave stray margins or borders on screen.
The lwc:else element must come directly after the lwc:if element. Any element placed between them breaks the pair and causes a compile error.
Telling "empty" from "not loaded yet"
An empty array can mean two things: the data has not arrived, or it arrived and has no records. Showing "No contacts to show." while a request is still running is misleading. A common approach is a second property that tracks loading.
isLoading = true;
contacts = [];
get showEmptyMessage() {
return !this.isLoading && !this.hasContacts;
}Set isLoading to false once the data has arrived. Then use showEmptyMessage for the fallback and a spinner or placeholder for the loading state.
Common errors
| Symptom | Likely cause |
|---|---|
| Compile error on `lwc:if` | An expression such as `contacts.length > 0` was used in the template |
| Runtime error reading `length` | The property is `undefined` or `null` and the getter has no guard |
| Fallback never appears | The condition tests the array itself, which is always truthy |
| Message flashes before data loads | No separate loading flag |
| Compile error on `lwc:else` | Another element sits between the `lwc:if` and `lwc:else` elements |
Exercise
A component receives accounts from a parent, and it may be undefined at first. Write a getter named isEmpty that returns true only when accounts is an array with no items. Why does lwc:if={accounts} fail to detect an empty list?
Show answer
get isEmpty() { return Array.isArray(this.accounts) && this.accounts.length === 0; } The template check lwc:if={accounts} fails because an empty array is truthy, so the check passes even when there are no items. A getter is also needed because the template cannot evaluate comparisons such as length === 0.