Development questions

How do components communicate in LWC: parent to child, child to parent, and between unrelated components?

DevelopmentMedium

Parent → Child: pass data through @api public properties, or call an @api method via this.template.querySelector('c-child').method().

Child → Parent: dispatch a CustomEvent; the parent listens with onmyevent.

javascript
this.dispatchEvent(new CustomEvent('select', { detail: { id: this.recordId } }));

Unrelated components (different DOM trees):

  • Lightning Message Service (LMS) – recommended; works across LWC, Aura and Visualforce.
  • A custom pub-sub module (legacy approach, same page only).

Use events for loose coupling and @api for direct control.

lwccommunicationevents