Sub topic 3
Communicating Upward and Extending LWC Components
Assume you know basic HTML, CSS, JavaScript classes, the DOM, and event listeners. This chapter focuses on how LWC components communicate, compose, render, and differ from browser-only web components.
Communicating upward with CustomEvent
A child component sends information to its parent with a DOM event. LWC uses the standard CustomEvent class. The child creates an event with a name and optional detail data.
import { LightningElement, api } from 'lwc';
export default class CounterButton extends LightningElement {
@api label = 'Add';
count = 0;
handleClick() {
this.count += 1;
this.dispatchEvent(
new CustomEvent('countchange', {
detail: { count: this.count }
})
);
}
}The parent listens with an on prefix:
<c-counter-button
label="Increase"
oncountchange={handleCountChange}>
</c-counter-button>The event name is countchange, so the handler attribute is oncountchange. Keep event names specific and send only the data the parent needs.
Composing components with slots
A component can expose a slot, which is a standard insertion point for child markup.
<template>
<section class="panel">
<slot></slot>
</section>
</template>A parent supplies content between the component’s tags. Named slots allow several insertion points. Slots support composition, while public properties configure behavior.
Lifecycle hooks and custom element callbacks
LWC lifecycle hooks correspond to custom element callbacks:
connectedCallback()runs when the component enters the DOM.renderedCallback()runs after rendering.disconnectedCallback()runs when it leaves the DOM.
Use these hooks for setup and cleanup. Avoid changing state repeatedly in renderedCallback(), because that can create a render loop.
Encapsulation and CSS scoping in practice
An LWC template has component boundaries. Its CSS targets elements in that component, and styles do not normally leak into a child component. This is similar to Shadow DOM encapsulation, although Salesforce’s compiler and runtime implement important details.
/* counterButton.css */
button {
border-radius: 0.25rem;
}The rule applies to buttons owned by this component, not arbitrary buttons elsewhere on the page.
Using Lightning base components
Lightning base components are Salesforce-provided components such as lightning-button and lightning-input. They provide accessible, platform-consistent behavior. Prefer them over recreating common controls.
What the compiler and framework add
The browser understands custom elements, modules, events, templates, and slots. LWC adds decorators such as @api, template directives such as for:each, reactive rendering, security integration, and Salesforce metadata deployment. The compiler converts LWC templates into efficient JavaScript.
Standard features versus LWC additions
| Capability | Browser standard | LWC addition |
|---|---|---|
| Custom elements and events | Yes | LWC conventions and framework integration |
| `@api` and template directives | No | Yes |
| Shadow-style boundaries | Yes | Compiler and runtime scoping |
| Lightning base components | No | Salesforce platform library |
Exercise
Which mechanism should a child use to tell its parent that a value changed?
Show answer
Dispatch a standard CustomEvent with a descriptive event name and place the relevant data in its detail property.