Sub topic 1
Fields in a Lightning web component
A Lightning web component keeps its state in class fields. The template reads those fields to decide what to show. This chapter covers where fields live, how a template reads them, and which rules apply before reactivity enters the picture.
Prerequisites
This chapter assumes you already know:
- Basic modern JavaScript: classes,
import/export, and spread syntax. - Basic HTML templates.
- The three-file structure of a Lightning web component (HTML, JavaScript, and a meta file).
- The difference between a primitive value (a number or string) and an object reference.
The files of a component
A component named counter lives in a folder of the same name. Two files matter for fields.
| File | Role |
|---|---|
| `counter.js` | Holds the class, its fields, and its methods |
| `counter.html` | Holds the template that reads fields and binds events |
| `counter.js-meta.xml` | Declares where the component can be used |
The meta file does not touch fields, so the rest of this chapter ignores it.
Declaring a field
A field is a variable declared directly in the class body. The class must extend LightningElement, which the framework provides.
import { LightningElement } from 'lwc';
export default class Counter extends LightningElement {
count = 0;
increment() {
this.count += 1;
}
}Three details are easy to miss:
- The declaration has no
let,const, orvar. It is justcount = 0;. - Inside methods you must write
this.count. A barecountrefers to a local variable, not the field. - A field with no initializer, such as
title;, starts asundefined.
Reading a field in the template
The template is plain HTML inside a <template> tag. Curly braces insert a field's value, and a handler name in braces binds an event to a method.
<template>
<lightning-button label="Add one" onclick={increment}></lightning-button>
<p>Count: {count}</p>
</template>In the template you write {count}, not {this.count}. The framework resolves the name against the component instance. Braces accept a property name or a dotted path such as {profile.name}. They do not accept expressions, so {count + 1} is invalid. To show a computed value, add a getter to the class:
import { LightningElement } from 'lwc';
export default class Counter extends LightningElement {
count = 0;
get nextCount() {
return this.count + 1;
}
increment() {
this.count += 1;
}
}The template can then use {nextCount}.
How the pieces connect
The class owns the data and the template displays it. A user action calls a method, the method changes a field, and the template shows the new value.
The step from this.count changing to the template reading it is the reactive part. The next chapters explain it in detail. For now, remember that the template never needs to be told about the change.
Fields are private by default
A field with no decorator belongs to the component alone. A parent component cannot read or set it, and it does not appear as an attribute in the parent's template. This is a deliberate default. State stays internal until you choose to expose it.
To let a parent set a field, you decorate it with @api. That is covered later in this tutorial.
A common naming habit is to prefix a private backing field with an underscore, such as _amount. The underscore has no special meaning to the framework. It only signals to readers that the field is internal, usually because a getter exposes it.
What to take forward
- Fields are declared in the class body and read with
this.in JavaScript and without it in the template. - The template can read a property name or dotted path, never an expression.
- Undecorated fields are private to the component.
Exercise
In the Counter component, a developer writes <p>Next: {count + 1}</p> in the template. Why does this fail, and how do you fix it?
Show answer
Template braces accept only a property name or dotted path, not an expression. Add a getter such as get nextCount() { return this.count + 1; } to the class and write <p>Next: {nextCount}</p>.
Exercise
Inside increment, a developer writes count += 1; without this.. What happens?
Show answer
A bare count is not the class field. It refers to a variable in scope, which does not exist, so the code throws a ReferenceError. Inside class methods you must write this.count += 1.