Sub topic 6
Computing conditions with getters
This chapter assumes you can read an LWC component's HTML template and JavaScript class, and that you know how to bind a property with curly braces. It also assumes you have used lwc:if on a simple property.
Why the template needs getters
The expression in lwc:if and lwc:elseif must be a simple reference to a property or getter. It cannot contain a comparison, a negation, or a function call. The template compiler rejects code like this:
<template>
<p lwc:if={status === 'error'}>Something went wrong.</p>
<p lwc:if={!isReady}>Not ready yet.</p>
<p lwc:if={items.length > 0}>You have items.</p>
</template>The fix is to move the logic into the JavaScript class. A getter is a method declared with get that the template reads like a property. The template binds to the getter name, and the class does the computing.
Writing a getter for a condition
Suppose a component tracks a status string. Each getter turns one comparison into a boolean.
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';
}
}The template then refers to the getters by name only.
<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>When handleRetry sets status, the component re-renders. LWC calls each getter again and re-evaluates the chain. Fields in LWC are reactive when the template uses them, and a getter that reads a reactive field is reevaluated when that field changes.
Common getter patterns
The same idea covers most conditions you will write:
| Condition you want | Getter body |
|---|---|
| A list has items | `return this.items.length > 0;` |
| A list is empty | `return !this.items.length;` |
| Value equals a string | `return this.status === 'error';` |
| Two things are both true | `return this.isLoggedIn && this.hasAccess;` |
| A record exists | `return !!this.record;` |
Getters can also guard against missing data. Reading .length on undefined throws an error, so check first:
get hasItems() {
return Array.isArray(this.items) && this.items.length > 0;
}<template>
<p lwc:if={hasItems}>You have {itemCount} items.</p>
<p lwc:else>Your list is empty.</p>
</template>Here itemCount would be another getter that returns this.items.length.
Keep getters small and cheap
A getter runs on every render, so avoid slow work inside it. Do not make server calls, and do not change state. A getter should only read values and return a result. If a computation is expensive, store the result in a field when the inputs change instead.
Give getters names that read as questions, such as isLoading, hasError, or showDetails. The template then reads almost like a sentence.
Common mistakes
- Writing a comparison or
!directly in the template. Use a getter instead. - Calling a getter with parentheses, as in
{isLoading()}. Bind the name only. - Reading a property that may be
undefinedwithout a guard. - Putting side effects, such as setting fields, inside a getter.
Exercise
A template contains <p lwc:if={items.length}>Items found</p>. Why does this fail, and how do you fix it?
Show answer
The lwc:if directive accepts only a simple property or getter reference, and items.length is an expression with member access. Add a getter such as get hasItems() { return this.items.length > 0; } to the class and bind lwc:if={hasItems}.