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

**Topic:** Lwc · **Difficulty:** Medium · **Tags:** lwc, communication, events

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-components-communicate-in-lwc-parent-to-child-child-to-parent-and-between-unrelated-components

## Answer

**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.

---
Source: ForceChallenges · last updated 2026-10-21
