Sub topic 11
Nesting Conditional Blocks
This chapter assumes you can write a single lwc:if chain, bind a getter to it, and know that lwc:else takes no value. Here you will place one conditional block inside another and learn when that is the right tool.
Why nest?
A chain picks one branch from a single set of conditions. Sometimes a branch needs its own second decision. For example, a panel first checks whether the user is signed in. Only inside the signed-in branch does it check whether the user has any orders. Those two questions belong at different levels, so the second chain goes inside the first branch.
Each chain is independent. A nested chain follows the same rules as a top-level one, and it is evaluated only when its parent branch renders.
A nested example
The template below checks the sign-in state first. Inside the signed-in branch, a second chain handles the order count.
<template>
<template lwc:if={isSignedIn}>
<h2>Welcome back, {userName}</h2>
<template lwc:if={hasOrders}>
<p>You have {orderCount} orders.</p>
</template>
<template lwc:else>
<p>You have not placed an order yet.</p>
</template>
</template>
<template lwc:else>
<p>Please sign in to continue.</p>
</template>
</template>The inner lwc:else belongs to the inner lwc:if. The outer lwc:else belongs to the outer lwc:if. Each lwc:else links to the directive that immediately precedes it among its own siblings.
The class supplies the properties and getters:
import { LightningElement } from 'lwc';
export default class AccountSummary extends LightningElement {
isSignedIn = true;
userName = 'Ada';
orderCount = 3;
get hasOrders() {
return this.orderCount > 0;
}
}When isSignedIn is false, the inner chain is never evaluated and none of its elements exist in the DOM.
Nesting versus a flat chain
You can often express the same logic with a flat chain and combined getters. Compare the two approaches.
| Approach | Best when | Cost |
|---|---|---|
| Nested chains | The inner decision only makes sense inside the outer branch, and the outer branch has shared markup such as a heading | More indentation, harder to scan when deep |
| Flat chain with combined getters | The outcomes are mutually exclusive states of one concept | Needs one getter per combination |
A flat version of the example needs three getters: signed out, signed in with orders, and signed in without orders. That works, but the shared heading would have to be repeated or moved. Nesting keeps the heading in one place.
Rules that still apply
lwc:elseifandlwc:elsemust be the next sibling of thelwc:iforlwc:elseifthey continue. Putting any element or text between them breaks the chain.- Conditions must be simple property references such as
{hasOrders}. Expressions like{orderCount > 0}are not allowed, so use a getter. - An inner chain cannot continue an outer one. They are separate chains even when they sit in the same file.
Lifecycle effect
Nested blocks follow the same create and destroy behavior as top-level ones. If isSignedIn flips to false, the whole signed-in branch is removed, including any child components in the inner chain. Those children run their disconnectedCallback. Flipping back creates new instances, so their state resets.
Exercise
A template has an outer lwc:if={isSignedIn} with a nested lwc:if={hasOrders} and lwc:else inside it. The user signs out while hasOrders is true. What happens to the inner "orders" block, and does hasOrders get evaluated again for rendering?
Show answer
The inner block is removed from the DOM because its parent branch no longer renders, and any child components inside it are disconnected. The inner chain is not rendered while isSignedIn is false, so its condition does not decide anything until the user signs in again. When they do, the inner block is created fresh.
Exercise
Rewrite <template lwc:if={isSignedIn}>...</template><p>Note</p><template lwc:else>...</template> so it is valid.
Show answer
Move the <p>Note</p> out from between the two directives, either before the lwc:if template or after the lwc:else template, or place it inside one of the branches. lwc:else must be the immediate next sibling of the lwc:if it continues.