Sub topic 10
Worked example: a loading, error, and ready panel
This chapter builds a status panel that shows one of three states: loading, error, or ready. It assumes you know how to write an LWC template and class, bind properties with curly braces, and use getters. It also assumes you know the basics of lwc:if, lwc:elseif, and lwc:else.
The goal
A component often waits for data. While it waits, it shows a loading message. If the request fails, it shows an error with a retry button. Otherwise it shows the data. Exactly one of these three blocks should be in the DOM at any time.
Keep one status field
Track the state in a single field instead of three separate booleans. One field cannot hold two values at once, so the panel cannot be "loading" and "error" together.
import { LightningElement } from 'lwc';
export default class StatusPanel extends LightningElement {
status = 'loading';
get isLoading() {
return this.status === 'loading';
}
get hasError() {
return this.status === 'error';
}
handleRetry() {
this.status = 'loading';
}
}lwc:if and lwc:elseif accept only a simple property reference such as {isLoading}. They do not accept expressions such as {status === 'loading'}. The getters turn each comparison into a property the template can read. Class fields are reactive, so assigning a new value to status makes the template re-evaluate the chain.
There is no getter for the ready state. It is the fallback, so lwc:else handles it.
The template
<template>
<template lwc:if={isLoading}>
<p>Loading...</p>
</template>
<template lwc:elseif={hasError}>
<p>Something went wrong.</p>
<button onclick={handleRetry}>Retry</button>
</template>
<template lwc:else>
<p>Data is ready.</p>
</template>
</template>The three blocks are siblings with nothing between them, so they form one chain. LWC checks them in order and renders the first branch whose condition is truthy. If none match, it renders the lwc:else block. Note that lwc:else has no value and no braces.
How a state change flows
When the user clicks Retry, handleRetry sets status to 'loading'. The component re-renders. isLoading is now true, so the first branch is rendered and the error block is removed from the DOM.
Connecting real work
In a real component, something other than the user changes status. A connectedCallback could start a request and set the status when it finishes. Here is a version that simulates this with a timer:
import { LightningElement } from 'lwc';
export default class StatusPanel extends LightningElement {
status = 'loading';
connectedCallback() {
this.load();
}
load() {
this.status = 'loading';
// Stand-in for a real data request.
setTimeout(() => {
this.status = Math.random() < 0.5 ? 'error' : 'ready';
}, 1000);
}
get isLoading() {
return this.status === 'loading';
}
get hasError() {
return this.status === 'error';
}
handleRetry() {
this.load();
}
}Each Retry click starts a new load. The panel shows "Loading..." and then lands on the error or ready block.
Common mistakes in this example
- Writing
lwc:if={status === 'loading'}. The template compiler rejects it. Use a getter. - Writing
lwc:else={isReady}.lwc:elsetakes no value. - Putting an element between the
lwc:ifblock and thelwc:elseifblock. This breaks the chain.
Exercise
You add a fourth state, 'empty', for when the request succeeds but returns no records. Which changes do you make to the JavaScript class and the template?
Show answer
In the class, add a getter such as get isEmpty() { return this.status === 'empty'; }. In the template, add <template lwc:elseif={isEmpty}><p>No records found.</p></template> between the existing lwc:elseif={hasError} block and the lwc:else block, with no other element in between. The lwc:else block then covers only the ready state.
Exercise
A teammate writes <template lwc:else={isReady}>. What is wrong, and how do you fix it?
Show answer
lwc:else does not take a value. Remove ={isReady} so it reads <template lwc:else>. The block renders whenever no earlier branch in the chain matched.