Sub topic 18
Standard features versus LWC additions
This chapter assumes you know the basics of custom elements, shadow DOM, ES modules and DOM events from earlier chapters. It sorts what you have seen into two groups: what the browser provides and what the LWC compiler and framework add.
Why the distinction matters
Standard features work in any web component and will keep working as browsers evolve. LWC additions only work inside the LWC compiler and runtime. Knowing which is which tells you where to look for answers. Use MDN for the standard parts. Use the LWC documentation for the rest.
What comes from the platform
- Custom elements. A component is registered as a custom element, so it is a real HTML element with a tag name.
- Shadow DOM. Markup and styles live in a shadow root, so outside CSS does not leak in.
- ES modules. Each component's JavaScript file is a module that uses
importandexport. - Events. Communication upward uses
CustomEventanddispatchEvent, and listeners use the normal event model. - Lifecycle basics.
connectedCallbackanddisconnectedCallbackmatch custom element callbacks. - Templates and slots. The
<template>and<slot>elements come from the HTML standard.
What LWC adds
- Template expressions and directives.
{name},if:true,for:eachandkeyare not browser features. The compiler turns the template into JavaScript. - The
@apidecorator. It marks a field or method as public. Plain custom elements have no such marker. - Field reactivity. Reassigning a class field re-renders the template. Plain classes do not do this.
- The component bundle. The folder with HTML, JavaScript and CSS files, plus the
-meta.xmlfile, is an LWC and Salesforce convention. - Handler syntax.
onchange={handleChange}in a template is compiled into a listener. - Base components. Components such as
lightning-buttonare a library, not part of the platform.
import { LightningElement, api } from 'lwc';
export default class CounterButton extends LightningElement {
@api label = 'Add'; // LWC addition: decorator
count = 0; // LWC addition: reactive field
handleClick() {
this.count += 1;
// Standard: CustomEvent and dispatchEvent
this.dispatchEvent(
new CustomEvent('countchange', { detail: { count: this.count } })
);
}
}The import line and the CustomEvent call are standard. The @api decorator, the reactive count field and the LightningElement base class belong to LWC.
Common mistakes
- Assuming
@apiorfor:eachworks in a hand-written web component. It does not. - Looking for
lightning-tags in MDN. They are not part of the browser. - Expecting the template syntax to run in the browser as written. The compiler must process it first.
Exercise
Sort these into "standard" or "LWC addition": CustomEvent, @api, <slot>, for:each.
Show answer
CustomEvent and <slot> are standard. @api and for:each are LWC additions.
Exercise
A teammate says reassigning this.count re-renders a plain web component class. What is wrong with that claim?
Show answer
Field reactivity comes from the LWC framework through LightningElement. A plain HTMLElement subclass does not re-render when a field changes.