DEV Community

Renuka Singh
Renuka Singh

Posted on

Salesforce LWC Communication: 5 Practical Ways Components Can Share Data

Lightning Web Components (LWC) help Salesforce developers build reusable and interactive user interfaces. In many applications, multiple components need to communicate with each other.

For example, a parent component may need to send information to a child component, or two separate components may need to share updates.

Salesforce provides different communication methods for these situations. Choosing the right method helps keep your components organized and easier to maintain.

Let's explore five practical ways to handle communication between Lightning Web Components.

  1. Parent-to-Child Communication Using Public Properties

The simplest way to pass data from a parent component to a child component is by using the @api decorator.

The parent component passes a value through an HTML attribute, and the child receives it through a public property.

Child component JavaScript:

import { LightningElement, api } from 'lwc';

export default class ChildComponent extends LightningElement {
@api message;
}

Child component HTML:

{message}


Parent component HTML:


message="Welcome to Salesforce LWC">

This approach is useful when a parent needs to provide information to its child component.

  1. Child-to-Parent Communication Using Custom Events

Sometimes, a child component needs to notify its parent about an action.

For example, a child component may contain a button that sends a selected value to the parent.

You can use custom events for this purpose.

Child component JavaScript:

import { LightningElement } from 'lwc';

export default class ChildComponent extends LightningElement {

handleClick() {
    const event = new CustomEvent('valuechange', {
        detail: 'New value selected'
    });

    this.dispatchEvent(event);
}
Enter fullscreen mode Exit fullscreen mode

}

Child component HTML:


label="Send Value"
onclick={handleClick}>

Parent component HTML:


onvaluechange={handleValueChange}>

<p>{selectedValue}</p>

Parent component JavaScript:

import { LightningElement } from 'lwc';

export default class ParentComponent extends LightningElement {

selectedValue;

handleValueChange(event) {
    this.selectedValue = event.detail;
}
Enter fullscreen mode Exit fullscreen mode

}

Custom events make it possible for child components to communicate with their parent without directly accessing the parent's internal properties.

  1. Communication Between Unrelated Components Using Lightning Message Service

What happens when two components are not in a parent-child relationship?

For example, one component displays customer information while another component shows related sales details.

Lightning Message Service (LMS) allows supported components to communicate through a shared message channel.

The general process is:

Create a Lightning Message Channel.

Import the channel into the publisher component.

Publish a message.

Subscribe to the channel in the receiving component.

Process the received information.

LMS is useful when components need to share information without being directly connected in the component hierarchy.

You can learn more in the official Lightning Message Service documentation.

  1. Share Data Through Lightning Data Service

Not every communication requirement needs a custom event or message channel.

Lightning Data Service (LDS) helps components access and work with Salesforce records.

For example, you can use getRecord to retrieve record information.

import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';

const FIELDS = ['Account.Name', 'Account.Phone'];

export default class AccountDetails extends LightningElement {

@api recordId;

@wire(getRecord, {
    recordId: '$recordId',
    fields: FIELDS
})
account;
Enter fullscreen mode Exit fullscreen mode

}

HTML:

Account Name: {account.data.fields.Name.value}


Phone: {account.data.fields.Phone.value}



LDS is useful when components need Salesforce record data and supported record operations. It also handles record caching and sharing of record data across components.

  1. Choose the Right Communication Method

Each communication method serves a different purpose.

Method

Best Use

@api properties

Parent to child

Custom events

Child to parent

Lightning Message Service

Unrelated components

Lightning Data Service

Salesforce record data

Shared JavaScript modules

Reusable logic and shared utilities

Before selecting a method, understand the relationship between the components and the type of information they need to exchange.

Common Mistakes to Avoid

When building LWC applications, developers should avoid a few common mistakes:

Using custom events when a public property is sufficient.

Trying to communicate between unrelated components through direct DOM access.

Creating unnecessary message channels.

Ignoring event names and payload structures.

Using custom Apex calls when Lightning Data Service already meets the requirement.

A simple communication structure makes components easier to test, reuse, and maintain.

Final Thoughts

Component communication is an important part of Salesforce Lightning Web Component development. Understanding public properties, custom events, Lightning Message Service, and Lightning Data Service helps developers choose the right solution for different application requirements.

For more Salesforce development guidance and implementation support, explore iBirds Software Services.

Top comments (0)