Sub topic 1
Quick recap: anatomy of a Lightning web component
This chapter reviews the parts of a Lightning web component (LWC) that the rest of the tutorial builds on. It is a recap, so it moves quickly.
Prerequisites
This chapter assumes you have:
- Basic HTML, CSS and modern JavaScript (classes, modules).
- Access to a Salesforce org or scratch org for trying the examples.
- A rough idea of what a component is: a reusable piece of user interface with its own markup, logic and styles.
The component bundle
A Lightning web component is a folder of files that share one name. The folder name becomes the component name. For a folder called contactSearch, other markup refers to the component as c-contact-search. The c is the default namespace, and the camelCase name becomes kebab-case.
| File | Purpose | Required |
|---|---|---|
| `contactSearch.html` | The template. It describes the markup. | Yes, if the component renders UI |
| `contactSearch.js` | The JavaScript class that holds state and handlers. | Yes |
| `contactSearch.css` | Styles that apply only to this component. | No |
| `contactSearch.js-meta.xml` | Configuration, such as where the component can be used. | Yes, to expose it in App Builder or Experience Builder |
The template
The template is an HTML file whose root element is <template>. Inside it you write ordinary HTML, other components, and bindings in curly braces. A binding like {searchKey} reads a property from the JavaScript class.
<template>
<lightning-card title="Contact search" icon-name="standard:contact">
<div class="slds-var-p-around_medium">
<lightning-input
type="search"
label="Name"
value={searchKey}
onchange={handleChange}
></lightning-input>
</div>
</lightning-card>
</template>Two things in this template matter for later chapters:
lightning-cardandlightning-inputare base components. You use them like HTML elements.slds-var-p-around_mediumis an SLDS utility class. It adds padding without any CSS written by you.
The JavaScript class
The class extends LightningElement. Its fields are the data that the template can read, and its methods are the handlers that the template can call.
import { LightningElement } from 'lwc';
export default class ContactSearch extends LightningElement {
searchKey = '';
handleChange(event) {
this.searchKey = event.target.value;
}
}Fields are reactive. When you assign a new value to this.searchKey, the component re-renders the parts of the template that use it. The onchange={handleChange} attribute in the template attaches handleChange as the event listener.
Note that you assign to this.searchKey and do not mutate it in place. For objects and arrays, assign a new object or array to trigger a re-render. Changing a property deep inside an object is tracked only in limited cases.
How the pieces connect
Data flows down from the class to the template through attributes. Events flow back up through handlers. Every base component in this tutorial follows this loop.
Styles in the bundle
A CSS file in the bundle applies only to that component's template. LWC uses the shadow DOM, so your selectors do not leak out, and outside styles do not leak in. A later chapter covers how SLDS classes and component CSS interact with this boundary.
Common errors
- Mismatched names. A file name that differs from the folder name stops the bundle from compiling.
- Wrong tag casing. Writing
<c-contactSearch>fails. Use<c-contact-search>. - Handler not found.
onchange={handleChange}needs a method of exactly that name on the class. - Missing metadata file. The component works in a parent component but does not appear in the page builders.
Exercise
In the example above, a user types "Ada" into the lightning-input. Describe which parts of the component run, in order, until the input shows the new value.
Show answer
The user's typing makes lightning-input fire a change event. The onchange attribute calls handleChange, which sets this.searchKey to event.target.value. The changed field makes the component re-render, and the template passes the new searchKey to the value attribute of lightning-input.
Exercise
A folder is named contactTable. What tag do you use to place it in another component's template?
Show answer
<c-contact-table></c-contact-table>. The c namespace is added, and the camelCase name is converted to kebab-case.