How to pass input value from parent to child component through LMS method in Salesforce | Hand-on example as pass input value from parent to child component through Lightning Message Service (LMS) in Salesforce

136 views

Hand-on example as pass input value from parent to child component through Lightning Message Service (LMS) in Salesforce

In Salesforce Lightning Message Service (LMS), we normally use it for communication between unrelated components (not directly parent–child).

But for learning purpose, we can still create a simple example where:

  • Parent Component publishes a message
  • Child Component subscribes and receives the value

This example is very beginner-friendly and easy to understand.

Step 1: Create Message Channel

First, define a message channel in your Salesforce org.

In your project directory, create a folder named messageChannels under force-app/main/default.

Inside the messageChannels folder, create a new XML file named SendMessageChannel.messageChannel-meta.xml.


🎁 Up to 99% Off Courses (Coupon)
πŸ’₯ Use Promo Code: STANDARDOFFERπŸ’₯
πŸš€ Get Free Salesforce Course Access: www.thevijaykumar.w3web.net

Add the following code to the file and deploy it to your org:

  1.  
  2.   <?xml version="1.0" encoding="UTF-8"?>
  3. <LightningMessageChannel xmlns="http://soap.sforce.com/2006/04/metadata">
  4.     <masterLabel>SendMessageChannel</masterLabel>
  5.     <isExposed>true</isExposed>
  6.     <description>Pass DATA FROM Parent TO Child USING LMS</description>
  7.  
  8.     <lightningMessageFields>
  9.         <fieldName>message</fieldName>
  10.         <description>Message Text</description>
  11.     </lightningMessageFields>
  12.  
  13. </LightningMessageChannel>

 

Step 2: Parent Component (Publisher)

This component will send input value using LMS.

parentComponent.html

  1.    <template>
  2.     <lightning-card title="Parent Component">
  3.  
  4.  
  5.         <div class="slds-grid slds-wrap slds-p-around_medium">
  6.             <div class="slds-col slds-size_6-of-12">
  7.  
  8.                 <lightning-INPUT label="Enter Message" VALUE={inputValue} onchange={handleChange}> </lightning-input>
  9.  
  10.                 <br/>
  11.  
  12.                 <lightning-button label="Send to Child" onclick={sendMessage}></lightning-button>
  13.  
  14.             </div>
  15.         </div>
  16.  
  17.     </lightning-card>
  18. </template>

 

parentComponent.js

  1.   import { LightningElement, wire } FROM 'lwc';
  2. import { publish, MessageContext } FROM 'lightning/messageService';
  3. import SEND_MESSAGE FROM '@salesforce/messageChannel/SendMessageChannel__c';
  4.  
  5. export DEFAULT class ParentComponent extends LightningElement {
  6.  
  7.     inputValue = '';
  8.  
  9.     @wire(MessageContext)
  10.     messageContext;
  11.  
  12.     handleChange(event){
  13.         this.inputValue = event.target.value;
  14.     }
  15.  
  16.     sendMessage(){
  17.  
  18.         const payload = {
  19.             message: this.inputValue
  20.         };
  21.  
  22.         publish(this.messageContext, SEND_MESSAGE, payload);
  23.     }
  24. }

 

Step 3: Child Component (Subscriber)

This component receives the value from parent.

  1.   <template>
  2.     <lightning-card title="Child Component">
  3.  
  4.         <p STYLE="padding:10px">
  5.             Received Message : {receivedMessage}
  6.         </p>
  7.  
  8.     </lightning-card>
  9. </template>

 

childComponent.js

  1.   import { LightningElement, wire } FROM 'lwc';
  2. import { subscribe, MessageContext } FROM 'lightning/messageService';
  3. import SEND_MESSAGE FROM '@salesforce/messageChannel/SendMessageChannel__c';
  4.  
  5. export DEFAULT class ChildComponent extends LightningElement {
  6.  
  7.     receivedMessage = '';
  8.  
  9.     subscription = NULL;
  10.  
  11.     @wire(MessageContext)
  12.     messageContext;
  13.  
  14.     connectedCallback(){
  15.         this.subscribeMessage();
  16.     }
  17.  
  18.     subscribeMessage(){
  19.  
  20.         IF(!this.subscription){
  21.  
  22.             this.subscription = subscribe(
  23.                 this.messageContext,
  24.                 SEND_MESSAGE,
  25.                 (message) => {
  26.                     this.receivedMessage = message.message;
  27.                 }
  28.             );
  29.         }
  30.     }
  31. }

 

Step 4: Add Both Components in One Page

app.html

  1.   <template>
  2.  
  3.     <c-parent-component></c-parent-component>
  4.  
  5.     <br/>
  6.  
  7.     <c-child-component></c-child-component>
  8.  
  9. </template>

 

app.js-meta.xml

  1.   <?xml version="1.0" encoding="UTF-8"?>
  2. <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
  3.     <apiVersion>45.0</apiVersion>
  4.     <isExposed>true</isExposed>
  5.     <targets> 
  6.         <target>lightning__AppPage</target>
  7.         <target>lightning__RecordPage</target>
  8.         <target>lightning__HomePage</target>
  9.     </targets>
  10. </LightningComponentBundle>

 

FAQ (Frequently Asked Questions)

What is @api and @track in lwc?

Lightning Web Components (LWC) introduced a new programming model with decorators that enhance the functionality of properties and functions. Among these decorators, @api, @track, and @wire play crucial roles in creating powerful and interactive Lightning components.

How to pass ref from parent to child React functional component?

forwardRef() is a React utility function that allows ref values from a parent component to be availed to a child functional component. The ref s are first created using useRef() or createRef() inside the parent. They are then forwarded and picked to manipulate a DOM node rendered inside the child JSX.

Which decorator is used to pass data from a parent component to a child component?

The @Input decorator is used to pass data from a parent component to a child component. Here the @Input decorator was used to mark the name property as an input property. If the value of the name property changes in the parent component, then that change is going to get propagated via the @Input() decorator.

Related Topics | You May Also Like

πŸ‘‰ Get Complementary β†’

right side banner -- www.w3webmart.com
 
  
πŸ‘‰ Get Free Course β†’

πŸ“Œ Salesforce Administrators

πŸ“Œ Salesforce Lightning Flow Builder

πŸ“Œ Salesforce Record Trigger Flow Builder

πŸ‘‰ Get Free Course β†’

πŸ“Œ Aura Lightning Framework

πŸ“Œ Lightning Web Component (LWC)

πŸ“Œ Rest APIs Integration



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net