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
More Development questions
- What is an Apex Trigger in Salesforce and when should you use it over Flows?
- What is the difference between before triggers and after triggers?
- Explain all Trigger context variables (isInsert, isUpdate, isDelete, isUndelete, isBefore, isAfter, isExecuting, size, operationType).
- What is the difference between Trigger.new and Trigger.old?
- In which trigger events are Trigger.new and Trigger.newMap available?
- In which trigger events are Trigger.old and Trigger.oldMap available?