Four paths for data
Properties down, events up, public methods for commands and LMS across the page. Choose the narrowest one that works.
- Down@api properties and setters.
- UpCustomEvent, detail, bubbles and composed.
- AcrossPublic methods and Lightning Message Service.
How components talk to each other is the most asked LWC topic. Know the four paths — properties down, events up, public methods and Lightning Message Service across the page — and when each is the right one.
1. How does a parent pass data to a child?
Through public @api properties set as attributes in the parent template (kebab-case in HTML, camelCase in JS). When the parent's value changes, the child re-renders. Use an @api setter to react to changes.
<!-- parent.html -->
<c-order-row order-id={selectedId} show-details></c-order-row>
// orderRow.js
_orderId;
@api
get orderId() { return this._orderId; }
set orderId(value) {
this._orderId = value;
this.loadOrder(value); // react to every change
}
@api showDetails = false;2. How does a child send data to its parent?
Dispatch a CustomEvent with data in detail; the parent listens with on plus the event name. Use lowercase event names without hyphens, and pass primitives or copies so the parent cannot mutate the child's internals.
// child: productTile.js
handleAdd() {
this.dispatchEvent(new CustomEvent('addtocart', {
detail: { productId: this.product.Id, qty: 1 }
}));
}
<!-- parent.html -->
<c-product-tile product={item} onaddtocart={handleAddToCart}></c-product-tile>
// parent.js
handleAddToCart(event) {
const { productId, qty } = event.detail;
}3. What do bubbles and composed do?
bubbles: true lets the event travel up the DOM; composed: true lets it cross shadow DOM boundaries. The default (both false) is the recommended pattern — the event reaches only the direct parent. Use bubbling with composed sparingly, since it exposes the event to components you do not control.
4. How does a parent call a method on a child?
Expose the method with @api in the child and call it via a reference (lwc:ref or querySelector) from the parent — for example to reset a form or refresh data.
<!-- parent.html -->
<c-filter-panel lwc:ref="filters"></c-filter-panel>
<lightning-button label="Clear" onclick={handleClear}></lightning-button>
// parent.js
handleClear() {
this.refs.filters.reset();
}
// filterPanel.js
@api reset() { this.city = ''; this.status = 'All'; }5. How do unrelated components communicate?
Use Lightning Message Service: define a message channel (XML metadata), publish from one component and subscribe in another — across the DOM, between LWC, Aura and Visualforce on the same Lightning page. Unsubscribe when the component is removed.
import { LightningElement, wire } from 'lwc';
import { publish, subscribe, unsubscribe, MessageContext } from 'lightning/messageService';
import ORDER_SELECTED from '@salesforce/messageChannel/OrderSelected__c';
export default class OrderList extends LightningElement {
@wire(MessageContext) messageContext;
select(id) { publish(this.messageContext, ORDER_SELECTED, { orderId: id }); }
}
export default class OrderDetail extends LightningElement {
@wire(MessageContext) messageContext;
subscription;
connectedCallback() {
this.subscription = subscribe(this.messageContext, ORDER_SELECTED, msg => this.load(msg.orderId));
}
disconnectedCallback() { unsubscribe(this.subscription); }
}6. Why should a child not change an @api property?
The parent owns the value; mutating it inside the child breaks one-way data flow (and objects passed in are read-only proxies). Copy it into a private field if you need a local editable version, and notify the parent with an event.
7. How do you communicate between an LWC and an Aura component?
Aura can contain LWC and listen to its events with on-handlers; Aura passes data to LWC through attributes. For siblings across frameworks, use Lightning Message Service.
8. Which method would you choose for these cases?
- Pass a record Id to a child list → @api property.
- Tell the parent an item was selected → CustomEvent.
- Ask a child form to validate before saving → @api method.
- Update a chart in another region of the page → LMS.
Remember
Key points
- Lowercase event names
onaddtocart, not onAddToCart.
- Default: no bubbling
Keep events between parent and child.
- LMS for unrelated components
Unsubscribe on disconnect.
- Do not mutate @api
Copy, then notify with an event.
Interview drill
Follow-up questions
What the interviewer asks after your first answer. Turn on practice mode, answer each one aloud, then reveal.
-
1 What is the pubsub module and is it still used? Intermediate
A sample library for publish/subscribe within one page before LMS existed. LMS is the supported replacement.
-
2 How do you pass data from LWC to a Flow? Intermediate
In a flow screen component, expose @api output properties in the meta XML and fire
FlowAttributeChangeEventwhen values change. -
3 Can events carry objects? Basic
Yes, in detail — but prefer primitives or copies; objects are shared references.
-
4 How do you stop an event in a parent? Basic
event.stopPropagation()in the handler. -
5 What are LMS scopes? Advanced
By default subscriptions receive messages only while the component is in an active area;
APPLICATION_SCOPEreceives messages from anywhere in the app. -
6 How do you send data to a sibling component? Basic
Child fires an event to the common parent, the parent updates a property bound to the sibling — or use LMS if they do not share a parent.