Sub topic 1
Properties Down, Events Up
Prerequisites
This chapter assumes you know basic JavaScript (classes, functions, objects, ES modules). It also assumes you can build a Lightning Web Component with an HTML template and a JavaScript class. You should be able to expose a public property with @api and place one component inside another in a template.
The two directions of communication
A Lightning web component (LWC) tree has parents and children. A parent contains a child in its template. Data and signals flow in different ways depending on direction:
- Down (parent to child): the parent sets public properties on the child. The child exposes them with
@api. - Up (child to parent): the child dispatches an event. The parent listens for it and reacts.
This split keeps each component in charge of its own state. A child never reaches into its parent to change a field. It announces that something happened, and the parent decides what to do.
Why the child cannot just call the parent
A child could be reused in many parents. If it called a specific parent's method, it would only work in that one place. An event removes that dependency. The child says "a product was selected" and does not care who listens. One parent may update a label, another may fetch data, and a third may ignore the event.
Events also keep the data flow easy to trace. Values come in through properties. Notifications go out through events.
Properties down: a quick look
Here is the parent setting values on a child. The child declares productId and name with @api, and the parent passes them as attributes.
<!-- productList.html -->
<template>
<c-product-tile product-id="p1" name="Desk"></c-product-tile>
</template>Notice that the attribute product-id in the template maps to the property productId in JavaScript. LWC converts kebab-case attribute names to camelCase property names.
A parent must treat the child as a black box. It sets only the public properties the child exposes.
Events up: the shape of the solution
The rest of this tutorial builds one example: a productTile child that tells a productList parent which product was picked. The pattern has four steps:
- The child creates a
CustomEventwith a name and an optional payload. - The child dispatches the event with
dispatchEvent. - The parent listens for the event in its template, using an
onprefix. - The parent handler reads the payload from
event.detailand updates its state.
Here is the whole loop in one view. Later chapters take each step apart.
// productTile.js
import { LightningElement, api } from 'lwc';
export default class ProductTile extends LightningElement {
@api productId;
@api name;
handleClick() {
this.dispatchEvent(
new CustomEvent('productselected', {
detail: { productId: this.productId }
})
);
}
}<!-- productList.html -->
<template>
<c-product-tile
product-id="p1"
name="Desk"
onproductselected={handleProductSelected}>
</c-product-tile>
<p>Selected: {selectedId}</p>
</template>The parent's handler stores the value, and the template re-renders:
// productList.js
import { LightningElement } from 'lwc';
export default class ProductList extends LightningElement {
selectedId;
handleProductSelected(event) {
this.selectedId = event.detail.productId;
}
}The event name productselected appears in two places. In the child it is the string passed to CustomEvent. In the parent it becomes the attribute onproductselected. If the two do not match exactly, the handler never runs, and no error is shown.
Common mistakes at this stage
- Mutating a property the parent passed in. Properties set through
@apiare read-only inside the child. To change a value, dispatch an event and let the parent update it. - Expecting the child to call a parent method. The child has no reference to its parent. Use an event.
- Mismatched names. A typo between the dispatched name and the
on...attribute fails silently.
Check your understanding
Exercise
A child component needs to tell its parent that a user closed a dialog. Should the child set a property on the parent, or dispatch an event? Explain in one or two sentences.
Show answer
It should dispatch an event. A child cannot set properties on its parent, and calling a specific parent would tie the child to one use. The child dispatches a CustomEvent, and the parent listens with an on-prefixed attribute and decides how to react.
Exercise
A parent template contains <c-product-tile onproductselect={handleSelect}>, but the child dispatches new CustomEvent('productselected'). Will handleSelect run? Why or why not?
Show answer
No. The listener attribute must be on plus the exact event name, so the parent needs onproductselected. The names differ, so the event is never delivered to handleSelect, and no error is raised.