Sub topic 8
Adding branches with lwc:elseif
This chapter assumes you can read an LWC template and a component class. It also assumes you know how lwc:if and lwc:else work, how getters feed template expressions, and how a property change triggers a re-render.
Why a second branch type
lwc:if and lwc:else give you two outcomes. Many screens have three or more. A data panel can be loading, failed, or ready. An order can be pending, shipped, or delivered. lwc:elseif adds a branch between the first and last ones, and you can use as many as you need.
The engine checks the branches from top to bottom. It renders the first branch whose condition is truthy and skips the rest. If no condition is truthy, the lwc:else branch renders. If the chain has no lwc:else, nothing renders.
The template
This template has three branches. Exactly one of them is in the DOM at any time.
<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>lwc:elseif takes a value in braces, like lwc:if. lwc:else takes no value. Writing lwc:else={something} is an error.
The class behind it
lwc:if and lwc:elseif accept only a property reference such as {isLoading}. They do not accept an expression like {status === 'loading'}. Use getters to turn comparisons into properties.
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';
}
}Fields in LWC are reactive. When status changes, the template re-evaluates the chain from the top. Clicking Retry sets status to 'loading'. The first branch becomes truthy, and the error block is removed from the DOM.
Order matters
Because the first truthy branch wins, the order of branches changes the result. Suppose a component can be loading and have an error at the same time, for example during a retry. Put lwc:if={isLoading} first, and the loading message shows. Put lwc:elseif={hasError} first in the chain, and the error shows.
Make conditions mutually exclusive where you can. Deriving all of them from one status field, as above, does this by design. A single source of truth prevents two flags from disagreeing.
Multiple elseif branches
A chain can hold any number of lwc:elseif branches. Here is a chain with several of them.
<template>
<p lwc:if={isPending}>Your order is pending.</p>
<p lwc:elseif={isShipped}>Your order is on its way.</p>
<p lwc:elseif={isDelivered}>Your order has arrived.</p>
<p lwc:else>Order status unknown.</p>
</template>Each directive here sits directly on a <p> element. That works the same as putting it on a <template> tag. Use a <template> tag when a branch holds several elements, so they render together without a wrapper element.
Placement rules
lwc:elseifmust come right after anlwc:ifor anotherlwc:elseif. The branches must be siblings.- Nothing may sit between the branches. Comments and whitespace are fine. Another element is not.
lwc:elsecomes last and closes the chain.- Do not put
lwc:ifandlwc:elseifon the same element.
Common mistakes
| Mistake | Result | Fix |
|---|---|---|
| `lwc:elseif={status === 'error'}` | Compile error, because only property references are allowed | Add a getter such as `hasError` |
| `lwc:else={isReady}` | Compile error, because `lwc:else` takes no value | Write `lwc:else` alone |
| An element between the `lwc:if` and `lwc:elseif` blocks | The chain breaks and `lwc:elseif` is rejected | Move the element outside the chain |
| Overlapping conditions | An earlier branch hides a later one | Reorder, or derive the flags from one field |
Exercise
A template has lwc:if={isLoading}, then lwc:elseif={hasError}, then lwc:else. At one moment both isLoading and hasError return true. Which block renders, and why?
Show answer
The loading block renders. The engine tests branches from top to bottom and renders the first truthy one, so isLoading wins. The hasError branch is never evaluated for rendering, and the lwc:else block is skipped.
Exercise
Write the getter that lets lwc:elseif show a block when a status field equals 'error'. Explain why the template cannot hold the comparison itself.
Show answer
get hasError() { return this.status === 'error'; } and then lwc:elseif={hasError}. Template directives accept only a property reference, not an expression, so the comparison must live in JavaScript.