Sub topic 2
Why Render Conditionally?
This chapter assumes you can write a basic Lightning Web Component (LWC), bind properties in a template with curly braces, and define a JavaScript getter. It explains why a template needs to decide what to show. The directives themselves come in later chapters.
The problem: one template, many situations
A component rarely shows the same thing all the time. A record list has data, no data, and a failed request. A form shows an extra field only when a box is checked. A card shows an edit button only to users who may edit.
A template is static markup with bindings. Without a way to choose, you would need one component per situation, or you would show everything and hope it makes sense. Conditional rendering lets one template describe several possible views. The component's state picks which view appears.
What conditional rendering means
Conditional rendering means the framework adds a block of markup to the DOM only when a condition is true. When the condition is false, the block is not in the DOM at all. The DOM is the live tree of elements the browser displays.
In LWC you attach a directive to a block. A directive is a special attribute that changes how the template renders. The directive lwc:if takes a property or getter. The engine reads its value each time the component renders and decides whether to include the block.
<template>
<p lwc:if={hasItems}>You have {itemCount} items.</p>
</template>import { LightningElement } from 'lwc';
export default class ItemSummary extends LightningElement {
itemCount = 3;
get hasItems() {
return this.itemCount > 0;
}
}With itemCount set to 3, the paragraph appears. Set it to 0 and the paragraph is removed. Nothing in the template changes. Only the state changes.
State drives the view
The key idea is that you do not tell the DOM to show or hide something. You change data, and the template reacts. This keeps the logic in one place.
The diagram below shows the flow.
Common reasons to render conditionally
- Loading and error states. Show a spinner while data loads, a message if the request fails, and the content when it succeeds.
- Empty states. Show a helpful message instead of a blank area when a list has no rows.
- Permissions and roles. Show actions only to users who can perform them.
- Progressive disclosure. Reveal details or extra inputs only when the user asks for them.
- Expensive children. Create a heavy child component only when it is needed.
The last item matters because a block that is not rendered does not exist. Its child components are not created, and they do not run their lifecycle hooks. A later chapter covers this in detail, and compares it with hiding content using CSS.
Where this fits in the chapters ahead
The rest of the plan builds on this motivation in steps:
- How a condition is evaluated, including truthy and falsy values.
lwc:if, thenlwc:elseandlwc:elseiffor choosing between branches.- Getters for conditions that are more complex than a single property.
- The rules for valid chains, nesting, and migrating from the older
if:trueandif:false. - What happens to child components when a branch toggles.
Check your understanding
Exercise
A component shows a spinner while data loads, an error message if the request fails, and a table when it succeeds. What does conditional rendering give you here, compared with putting all three blocks in the template unconditionally?
Show answer
Only the block that matches the current state is in the DOM. The component's state, such as a loading or error field, selects the block, and changing that state makes the template re-render. Without conditions, the user would see the spinner, the error, and the table at the same time.
Exercise
When the condition on a conditional block becomes false, is the block still in the DOM, and what does that imply for any child components inside it?
Show answer
No. The block is removed from the DOM, so child components inside it are not rendered and their lifecycle hooks do not run while it is absent.