Sub topic 4
The JavaScript Class
Prerequisites
This chapter assumes you know ES modules, JavaScript classes, and the bundle layout from the earlier chapters. Specifically, it assumes the folder and file naming rules and the role of the HTML template. You should also know what a Salesforce DX project looks like.
What the file does
The JavaScript file holds a component's behavior. It defines the data the template displays and the logic that responds to events. It also defines which values a parent component can set.
The file must have the same base name as the folder. For the greetingCard folder, the file is greetingCard.js. Lightning Web Components (LWC) looks for this file by name. If the name differs, the component does not load.
The minimum required code
The file must default-export a class that extends LightningElement.
import { LightningElement, api } from 'lwc';
export default class GreetingCard extends LightningElement {
@api name = 'World';
}Each part has a job:
import { LightningElement, api } from 'lwc'loads the base class and the decorator from thelwcmodule.export defaultmakes the class the module's single default export. The framework imports exactly that.extends LightningElementgives the class the component behavior: lifecycle hooks, thetemplateandshadowRootaccess points, and event methods.@apiis a decorator. It marksnameas a public property.
The class name is by convention the PascalCase form of the folder name. The framework does not use it to find the component. It uses the file name.
Public and private fields
Fields in the class are reactive. When a field's value changes and the template uses it, the component rerenders.
Fields come in two kinds:
| Kind | Declared as | Who can set it |
|---|---|---|
| Public property | `@api name` | A parent component, or App Builder if the configuration file allows it |
| Private field | `count = 0` | Only the class itself |
A parent sets a public property through the markup. A child must not change a value it received through @api. Treat it as read-only inside the child. To change data, the child sends an event to the parent.
import { LightningElement, api } from 'lwc';
export default class Counter extends LightningElement {
@api label = 'Clicks';
count = 0;
handleClick() {
this.count += 1;
}
}In the template, {count} updates each time handleClick runs. label can also be set from outside.
How the file connects to the others
The framework pairs the class with the HTML file of the same base name. Names in curly braces, such as {name}, refer to fields and getters on the class. Event handlers such as onclick={handleClick} refer to methods. The next chapter covers this binding in detail.
The class can also import other files from the bundle. A bundle can hold helper modules, and the class can import them by relative path. Another section of this tutorial covers that.
Common errors
- Wrong export. A named export, such as
export class GreetingCard, fails. Useexport default. - Wrong base class. A class that does not extend
LightningElementis not a component. - File name mismatch.
greeting-card.jsin agreetingCardfolder does not work. The base name must match the folder. - Missing import. Using
@apiwithout importingapifromlwcgives a compile error. - Changing an
@apivalue inside the child. This leads to unpredictable behavior. Copy the value into a private field, or send an event.
Exercise
Exercise
A developer creates a folder orderSummary with a file orderSummary.js that contains export class OrderSummary extends LightningElement { @api total; }. The component does not load. Name two problems and give the corrected code.
Show answer
First, the class uses a named export, but the framework needs a default export. Second, the file does not import LightningElement or api from 'lwc'. The corrected code is:
import { LightningElement, api } from 'lwc';
export default class OrderSummary extends LightningElement {
@api total;
}