Sub topic 1
Quick refresher: binding properties in a template
Conditional rendering depends on one mechanism: a template reads a value from the component's JavaScript class, and the DOM updates when that value changes. This chapter reviews that mechanism before the next chapters build lwc:if on top of it.
Prerequisites
This chapter assumes you already know:
- Basic HTML and JavaScript, including ES modules, classes, and getters.
- The structure of a Lightning Web Component: an HTML template and a JavaScript class that share a folder and a name.
- How to write
{property}in a template andonclick={handler}on an element.
If any of these are unfamiliar, review them first. The rest of this tutorial uses them without explanation.
How binding works
A curly-brace expression in the template refers to a member of the component class. The engine reads that member when it renders the template. When the member changes, the engine renders again and updates only the parts of the DOM that differ.
<template>
<p>Hello, {greeting}!</p>
<button onclick={handleClick}>Change</button>
</template>import { LightningElement } from 'lwc';
export default class Greeter extends LightningElement {
greeting = 'World';
handleClick() {
this.greeting = 'LWC';
}
}Clicking the button assigns a new value to greeting. The component re-renders, and the paragraph shows "Hello, LWC!".
Reactive fields
Every class field is reactive. Reassigning a field, as in this.greeting = 'LWC', triggers a re-render if the template uses that field.
Objects and arrays need more care. The engine detects reassignment of the field itself, not changes inside the value. Changing a property of a plain object does not trigger a re-render. Replace the whole object instead:
import { LightningElement } from 'lwc';
export default class Profile extends LightningElement {
user = { name: 'Ana', role: 'Admin' };
promote() {
// Reassign so the template notices the change.
this.user = { ...this.user, role: 'Owner' };
}
}In the template, {user.name} reads a property of that object. Dot notation is allowed. Calls, comparisons, and arithmetic are not.
Getters for computed values
Template expressions must be simple references. The template cannot contain {count + 1} or {status === 'error'}. Put the computation in a getter and bind the getter.
import { LightningElement } from 'lwc';
export default class Counter extends LightningElement {
count = 0;
get label() {
return `Clicked ${this.count} times`;
}
increment() {
this.count += 1;
}
}<template>
<button onclick={increment}>{label}</button>
</template>The getter runs again each time the component renders, so it always reflects the current count. The next chapters rely on this pattern. lwc:if accepts only a simple reference, so every comparison you want to test will live in a getter.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| The template shows nothing for `{value}` | The field name is misspelled, or the member does not exist on the class | Match the name in the class exactly, including case |
| A nested object change does not update the UI | The object was mutated, not reassigned | Assign a new object or array to the field |
| A compile error on `{a === b}` | The expression is not a simple reference | Move the logic into a getter |
| The handler does not run | You wrote `onclick={handleClick()}` or used a quoted value | Write `onclick={handleClick}` without parentheses or quotes |
Exercise
A component has the field items = ['a', 'b']. A handler runs this.items.push('c'), and the template iterates over items. The list does not update. What is the fix, and why does it work?
Show answer
Reassign the field with a new array, for example this.items = [...this.items, 'c'];. The engine tracks reassignment of the field. A push changes the contents of the existing array, so the engine never sees a new value and does not re-render.