SalesforceWizard

LWC events and communication interview questions

"How do two components talk to each other?" is asked in almost every LWC round. Here is the full answer, from custom events to Lightning Message Service.

0 of 10 reviewed
On this page (6 sections)Interview questionsCommon MistakesRelated LWC Events Interview QuestionsFrequently Asked Questions about LWC Events InterviewsRelated CertificationsRelated Resources

Event communication is the bread and butter of any non-trivial LWC project. Interviewers love testing whether you understand shadow DOM, composed events, and when to reach for Lightning Message Service.

01

How does communication work between LWC components?

Three patterns: Parent → Child via @api properties and methods. Child → Parent via CustomEvent dispatched from child, listened on parent. Sibling/unrelated → use Lightning Message Service (LMS) which is the modern pub-sub channel.

02

How do you fire a custom event from child?

this.dispatchEvent(new CustomEvent('selected', { detail: { id: this.recordId }, bubbles: false, composed: false })). Detail carries the payload. Use kebab-case for event names: Salesforce convention.

03

What are bubbles and composed?

bubbles: event travels up the DOM tree. composed: event crosses shadow DOM boundaries. Default is false/false. Set both true only when you genuinely need cross-component bubbling: keep events scoped tight by default.

04

What is Lightning Message Service (LMS)?

Pub-sub channel for unrelated components, including Aura ↔ LWC ↔ Visualforce communication on the same page. Define a Message Channel in metadata, then publish/subscribe via @wire(MessageContext) and publish/subscribe APIs.

05

Difference between LMS and pubsub.js?

LMS is the official, supported way. The old pubsub.js library is community code that worked only inside the same page+namespace. LMS works cross-namespace and cross-tech. Always use LMS in new projects.

06

How does parent listen to child event?

<c-child onselected={handleSelected}></c-child>. Note 'on' + event name, all lowercase. Handler receives the event object: read event.detail for payload.

07

How do you stop event propagation?

event.stopPropagation() inside the handler. Useful when nested handlers would react to the same event.

08

Can a parent call a method on a child?

Yes. Define method on child with @api, then call from parent via this.template.querySelector('c-child').myMethod(). Use sparingly; events are usually cleaner.

09

How do you debug an event that's not firing?

Console.log inside the dispatchEvent and the parent handler. Verify event name matches exactly. Check shadow DOM boundary: if the listener is across shadow DOM, you need composed:true.

10

Real-world LMS use case?

A list LWC and a detail LWC sitting on the same record page need to talk without being parent-child. List publishes a 'recordSelected' message; Detail subscribes and re-fetches data. This is how console-style apps are built.

Common Mistakes

Always setting composed:true. Pollutes the DOM tree with cross-boundary events.

Using pubsub.js in new projects. LMS is the supported way.

Mutating event.detail data. Freeze it before dispatching.

Frequently Asked Questions about LWC Events Interviews

Can LWC dispatch standard DOM events?

Yes, like 'click' or 'change', but use CustomEvent for component-to-component communication.

Are events synchronous or async?

Synchronous: handlers run immediately when dispatchEvent is called.

Can I pass an SObject in event detail?

Yes, but freeze it (Object.freeze) so receivers don't mutate the source.

Do LMS messages persist across page navigation?

No, they're page-scoped.

Can two LWCs in different tabs communicate?

Not via LMS: use BroadcastChannel API or a server round-trip.

JavaScript Developer I and PD2. See certification guides.

Chat on WhatsApp