Sub topic 4
Showing content with lwc:if
This chapter assumes you can build a basic Lightning Web Component. You should know how to bind a property in a template with curly braces, and you should be comfortable with JavaScript classes and getters. You should also know how to handle events in a component.
What lwc:if does
lwc:if is a template directive that adds an element to the DOM when its condition is truthy and leaves it out when the condition is falsy. The value must be a property reference in curly braces. It can be a field or a getter on the component class.
<template>
<p lwc:if={isVisible}>The panel is open.</p>
</template>import { LightningElement } from 'lwc';
export default class Panel extends LightningElement {
isVisible = true;
}When isVisible changes, the framework re-evaluates the condition and updates the DOM. The element is created or removed. It is not just hidden.
Truthy and falsy values
The directive does not require a boolean. JavaScript decides whether the value is truthy or falsy.
| Value | Result |
|---|---|
| `true`, non-zero numbers, non-empty strings, objects, arrays | Renders |
| `false`, `0`, `""`, `null`, `undefined`, `NaN` | Does not render |
An empty array is still an object, so it is truthy. lwc:if={items} renders even when items is []. Test the length instead, as shown in the getter section below.
Template tags and elements
You can put lwc:if on a <template> tag to control a group of elements, or directly on one element.
<template>
<template lwc:if={isVisible}>
<h2>Account details</h2>
<p>Name: {accountName}</p>
</template>
<button lwc:if={canEdit} onclick={handleEdit}>Edit</button>
</template>Use the <template> form when several siblings share one condition. It adds no wrapper element to the DOM, so your layout and CSS are unchanged. Use the element form when only one element is conditional.
Computing conditions with getters
The directive accepts only a simple property reference. Expressions such as lwc:if={items.length > 0} or lwc:if={!isDone} are not allowed. Move the logic into a getter.
import { LightningElement } from 'lwc';
export default class ItemList extends LightningElement {
items = [];
get hasItems() {
return this.items.length > 0;
}
get itemCount() {
return this.items.length;
}
handleAdd() {
this.items = [...this.items, { id: this.items.length + 1 }];
}
}<template>
<p lwc:if={hasItems}>You have {itemCount} items.</p>
<button onclick={handleAdd}>Add item</button>
</template>The getter reads this.items. Clicking the button assigns a new array to items, which triggers a re-render and re-runs hasItems.
How the update flows
A common mistake
Putting a comparison or negation in the braces is the most frequent error. It fails at compile time, not silently, so the message points you straight to the line.
Exercise
A component has items = [] and the template uses <p lwc:if={items}>Results found</p>. Does the paragraph render? How would you fix the condition?
Show answer
It renders, because an empty array is an object and therefore truthy. Add a getter get hasItems() { return this.items.length > 0; } and use lwc:if={hasItems}.
Exercise
You need to show a heading and a paragraph together under one condition without adding a wrapper div. Which form of the directive do you use?
Show answer
Place lwc:if on a <template> tag that contains both elements. The <template> tag adds no element to the DOM.