Sub topic 6
What Lightning Web Components are
This chapter assumes you know basic HTML and CSS, JavaScript classes, and the browser DOM and event listeners. It covers what Lightning Web Components (LWC) are and why they exist. Later chapters cover the bundle, templates, events and lifecycle in detail.
What LWC is
Lightning Web Components is a framework for building web components. It runs on the Salesforce platform and also ships as the open-source LWC project. You write a component as an HTML template and a JavaScript class. The framework compiles it into code that runs in the browser as a custom element.
LWC exists because browsers now provide most of what older frameworks had to invent: custom elements, shadow DOM, templates, slots and ES modules. LWC uses these standards and adds a thin layer on top. That layer covers the template syntax, reactivity and decorators such as @api.
The same idea with and without LWC
A plain web component registers a class that extends HTMLElement and renders into a shadow root.
class HelloGreeting extends HTMLElement {
connectedCallback() {
const root = this.attachShadow({ mode: 'open' });
const name = this.getAttribute('name') ?? 'World';
root.innerHTML = `<p>Hello, ${name}!</p>`;
}
}
customElements.define('hello-greeting', HelloGreeting);In LWC, the template is a separate file. It uses a root <template> element and a {name} expression bound to a class field.
<!-- helloGreeting.html -->
<template>
<p>Hello, {name}!</p>
<input type="text" value={name} onchange={handleChange} />
</template>The class is an ES module that extends LightningElement. Reassigning the field name re-renders the template.
// helloGreeting.js
import { LightningElement } from 'lwc';
export default class HelloGreeting extends LightningElement {
name = 'World';
handleChange(event) {
this.name = event.target.value;
}
}You never call customElements.define or attachShadow yourself. The framework does both. The folder name helloGreeting becomes the tag c-hello-greeting when another component uses it.
How the pieces map
| LWC piece | Web standard it relies on |
|---|---|
| Component class extending `LightningElement` | Custom element (`HTMLElement`) |
| Rendered markup and scoped styles | Shadow DOM |
| `<template>` and `<slot>` in the HTML file | HTML template and slot elements |
| `import` and `export default` | ES modules |
| `dispatchEvent(new CustomEvent(...))` | DOM events |
Platform configuration
On Salesforce, a helloGreeting.js-meta.xml file in the bundle sets the API version and where the component can be placed.
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
</targets>
</LightningComponentBundle>This file is specific to the Salesforce platform. The open-source LWC project does not need it.
Common mistakes
- Expecting the template to be plain HTML. Expressions and event handlers only work after compilation.
- Calling
attachShadowin an LWC class. The framework already manages the shadow root. - Forgetting the meta file on Salesforce. Without
isExposedandtargets, you cannot place the component in App Builder.
Exercise
In the LWC example, which two things does the framework do for you that the plain web component example does by hand?
Show answer
It registers the class as a custom element (the customElements.define call) and creates the shadow root (the attachShadow call).