Sub topic 1
Properties down, events up
Every Lightning Web Components (LWC) application is a tree of components. Parents render children, children render their own children, and data has to move between them. LWC gives you one direction for each kind of traffic. Data flows down the tree through public properties. Signals flow up the tree through events. This chapter explains that model, why it exists, and where it can go wrong. Later chapters cover the event mechanics in detail.
Prerequisites
This chapter assumes you already know the following:
- Basic JavaScript: classes, functions, objects, and ES modules.
- The structure of an LWC component: an HTML template and a JavaScript class that extends
LightningElement. - How to expose a public property with
@api. - How to place one component inside another in a template, such as
<c-product-tile>.
You don't need any prior experience with CustomEvent or dispatchEvent.
The two directions
A component's public API has two sides. The inputs are the properties (and methods) it marks with @api. The outputs are the events it dispatches. A parent that uses a child sets the inputs and listens for the outputs. The child knows nothing about who its parent is.
| Direction | Mechanism | Who initiates | Who knows about whom |
|---|---|---|---|
| Down (parent to child) | `@api` property set from the parent template | Parent | Parent knows the child's property names |
| Up (child to parent) | `CustomEvent` dispatched by the child | Child | Parent knows the child's event names. The child knows nothing about the parent. |
The second row is the point of the pattern. A child that dispatches productselected works the same way whether the parent saves the selection, shows a toast, or ignores the event. The child can be reused anywhere because it doesn't reach into its surroundings.
Properties down
A parent passes data to a child by setting attributes on the child's tag. Attribute names are written in kebab-case (words separated by hyphens). LWC maps them to the camelCase names declared in the child's class. So product-id in the parent template sets productId in the child.
<!-- productList.html -->
<template>
<c-product-tile product-id="p1" name="Desk"></c-product-tile>
</template>// productTile.js
import { LightningElement, api } from 'lwc';
export default class ProductTile extends LightningElement {
@api productId;
@api name;
}When the parent later changes the value bound to one of these attributes, the child receives the new value and re-renders. This is the full data-down mechanism. Setting a property is a one-way push.
The child must treat its inputs as read-only
LWC enforces a rule: a component must not modify the values it receives through @api properties. The rule has two practical effects.
- If the value is an object or array, the child receives a read-only view of it. Changing a field on it, such as
this.product.name = 'x', throws an error in the browser console. - If the value is a primitive such as a string, the child can technically assign to the property, but the parent's value does not change. The two components now disagree about the value. This is a bug source, and you should treat it as forbidden.
If a child wants a different value, it can't write that value upward. It has to ask the parent to change it. That request is an event.
Events up
A child tells its parent that something happened by creating a CustomEvent and dispatching it on itself. A CustomEvent is a browser-standard event object that lets you choose the event name and attach your own data in a detail property. Here is the complete child side. The next chapters take it apart piece by piece.
// 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 }
})
);
}
}<!-- productTile.html -->
<template>
<button onclick={handleClick}>{name}</button>
</template>The parent listens by putting on in front of the event name and using the result as an attribute on the child's tag:
<!-- productList.html -->
<template>
<c-product-tile
product-id="p1"
name="Desk"
onproductselected={handleProductSelected}>
</c-product-tile>
<p>Selected: {selectedId}</p>
</template>// productList.js
import { LightningElement } from 'lwc';
export default class ProductList extends LightningElement {
selectedId;
handleProductSelected(event) {
this.selectedId = event.detail.productId;
}
}The parent stores the payload in its own field, and the template re-renders. The child never changed anything in the parent. It reported a fact, and the parent decided what to do with it.
A full round trip
Put both directions together and you get a loop. The parent sets properties, the user interacts with the child, the child reports the interaction, and the parent updates its state, which flows back down as new properties.
This loop is why LWC components stay predictable. State has one owner, and you can find it by walking up from the component that displays it. Changes to that state arrive as events, so each change has a visible entry point, which is the handler.
Why LWC uses events for this
You could imagine a child calling a method on its parent directly, or writing into a shared object. LWC rules these out for several reasons.
- Encapsulation. The child has no reference to its parent. A component can be used in a different parent, in a test, or in an app builder without code changes.
- Shadow DOM boundaries. Each component renders inside its own shadow tree, a private DOM subtree. By default, events stop at the boundary of that tree unless you opt in to more. This is the same isolation that keeps CSS and
querySelectorcalls from leaking between components. - Standards alignment. LWC events are DOM events. The same
addEventListener,dispatchEvent, andCustomEventthat work on a<div>work on your components. You aren't learning a framework-specific messaging system. - Testability. To test a child, you attach a listener to it and check what arrives. You don't need a parent at all. A later chapter shows this with Jest.
Common errors at this boundary
Most bugs here come from mixing up the two directions. These are the ones you'll meet first.
- Mutating an
@apivalue in the child. You see an "invalid mutation" style error for objects, or silent disagreement for primitives. Fix: dispatch an event and let the parent change its data. - Event name mismatch. The child dispatches
productSelectedbut the parent listens withonproductselected. Nothing fires and nothing throws. The names have to match exactly, and the next chapters cover the naming rules that keep this simple. - Putting the listener on the wrong element.
onproductselectedmust be on the tag of the component that dispatches the event, not on a wrapper or on the parent's own root. - Dispatching from the wrong place. Dispatching during the constructor is not allowed, because the component has no host element to dispatch on yet. Dispatch in response to a user action, a lifecycle hook such as
connectedCallback, or a promise result. - Sending a heavy or mutable payload. The
detailobject is shared by reference with every listener. Send small, plain data, and don't put a reference to your own internal state in it.
What this chapter does not cover
Properties and events handle the parent-child relationship. They are not the only tools. A parent can call a public @api method on a child to give it a command. Components that are not in the same branch of the tree, such as two siblings or distant cousins, usually need a shared mechanism such as Lightning Message Service, a publish-subscribe module, or data-fetching features like wire adapters. Reaching for events across a long chain of components often means the design needs a different pattern. The final chapters return to this decision.
For now, hold on to the rule of thumb: if the data goes from owner to user, use a property. If something happened and the owner might care, use an event.
What comes next
The next chapter looks at CustomEvent itself, including its constructor, its options, and what an event object contains. After that come dispatching, naming rules, the detail payload, handling in the parent, programmatic listeners, bubbles and composed, and testing with Jest.
Exercise
A child component c-quantity-picker has @api quantity = 1 and a button that should increase the quantity. A developer writes this.quantity = this.quantity + 1 in the button handler, and the parent's displayed quantity never changes. Explain why, and describe the correct approach.
Show answer
The parent owns the value and passes it down as a property. When the child assigns to this.quantity, the change only affects the child's copy, so the parent's value stays the same and the two components disagree. The child should dispatch a CustomEvent (for example quantitychange) with the requested value in detail. The parent should handle it, update its own field, and let the new value flow back down through the @api property.
Exercise
In the parent template you write <c-product-tile onProductSelected={handleSelect}>, but the child dispatches new CustomEvent('productselected'). Nothing happens when the button is clicked. Identify the cause and the fix.
Show answer
The attribute name does not match the event name. HTML attributes in LWC templates are lowercase, so onProductSelected does not map to the event productselected. The fix is to write onproductselected={handleSelect}, which is on followed by the exact event name.