- SalesforceChaCha
- Posts
- 💃 LWC Communication Playbook 🕺
💃 LWC Communication Playbook 🕺
How to relay information between components
Good morning, Salesforce Nerds!
Lightning Web Components (LWCs) are like the ultimate social network of Salesforce development - they’re all about communication! 📣
But not all conversations are the same.
Some are formal, some are casual, and some require a little third-party intervention. 🤷
So, let’s dive into the communication types between LWCs and how to master them, all while having some fun along the way.

TABLE OF CONTENTS
LWC Communication Playbook
YOUR TICKET TO THE EVENT
USING EVENTS TO COMMUNICATE
HTML DOM events allow JavaScript to register different event handlers on elements in an HTML document. 📄
Events are typically used with functions, and the function will not be executed before the event occurs (such as when a user clicks a button).
DOM events are a fundamental mechanism for communication between Lightning Web Components. 💯
By leveraging the browser’s native event system, components can seamlessly communicate without requiring direct relationships. 🤙
This is especially useful for Child-to-Parent communication, where custom events can be dispatched from a child and bubbled up through the DOM until caught by an ancestor component.
With a consistent structure and reliance on standards, DOM events make it easier to build flexible, maintainable applications that adapt to changing requirements. 📦️
THE ROYAL DECREE
PARENT-TO-CHILD COMMUNICATION
When it comes to Parent-to-Child communication, think of the parent component as the king or queen of the castle. 🏰
It’s in charge and hands down orders to its child components.
In LWC terms, this is done using public properties or @api decorators.
Example: Let’s say you have a parent component that wants to send a friendly greeting to its child: 👋
Parent Component (HTML):
<c-child-component greeting="Hello, little one!"></c-child-component>
Child Component (JavaScript):
import { LightningElement, api } from 'lwc';
export default class ChildComponent extends LightningElement {
@api greeting;
}
What’s Happening? The parent passes data (in this case, greeting
) as an attribute to the child component.
The child can then access it via the @api
decorated property.
It’s clean, it’s simple, and it’s royalty-approved! 👑
Pro Tip: Keep it simple. Don’t overburden child components with too many inputs. It’s a one-way street, so let it flow smoothly.
THE CRY FOR HELP
CHILD-TO-PARENT COMMUNICATION
Kids have a knack for letting their parents know what’s up, whether it’s with a gentle nudge or a full-blown tantrum. 😭
In LWCs, this dynamic is mirrored using Custom Events.
Example: Imagine a child component that’s tracking user clicks and needs to inform its parent: 🗣️
Child Component (JavaScript):
import { LightningElement } from 'lwc';
export default class ChildComponent extends LightningElement {
handleButtonClick() {
const myEvent = new CustomEvent('buttonclick', {
detail: { clicked: true }
});
this.dispatchEvent(myEvent);
}
}
Child Component (HTML):
<button onclick={handleButtonClick}>Click Me!</button>
Parent Component (HTML):
<c-child-component onbuttonclick={handleButtonClick}></c-child-component>
Parent Component (JavaScript):
import { LightningElement } from 'lwc';
export default class ParentComponent extends LightningElement {
handleButtonClick(event) {
console.log('Child says:', event.detail);
}
}
What’s Happening? The child dispatches a custom event (buttonclick
), and the parent listens for it.
This makes the child’s message heard loud and clear. 🔊
Perfect for when the child needs to grab the parent’s attention.
Pro Tip: Use descriptive event names and include useful details in the detail
property. It’ll make debugging and extending functionality much easier.
THE OFFICE GOSSIP
WRAPPING IT UP
TAKEAWAY
From royal decrees to cries for help and office gossip, Lightning Web Components offer robust ways to communicate. 🔥
Understanding and leveraging these patterns ensures your components work harmoniously, creating a seamless user experience.
Now go forth and let your components talk it out - they’ve got a lot to say! 😀
SOUL FOOD
Today’s Principle
"The single biggest problem in communication is the illusion that it has taken place."
and now....Salesforce Memes



What did you think about today's newsletter? |