Hey guys, today in this post we are going to learn about In LWC communication how to pass the multiple values from parent component to child component through component JavaScript Method in Salesforce lightning web component.
Files we used in this post example
parentCmpLwc.html | Lightning Web Component HTML | The Templae HTML file for used to write HTML element for user interface. |
parentCmpLwc.js | Lightning Web Component JavaScript | It is holding click functionality and geting the input value from child component. |
parentCmpLwc.js-meta.xml | XML Meta File | It is used for where this lightning web component you want to exposed. |
childCmpLwc.html | Lightning Web Component HTML | It is child HTML file and calling on parentCmpLwc.html. |
childCmpLwc.js | Lightning Web Component JavaScript | It is holding @api public property and passing the value into parent componet. |
lwcApp.app | Lightning Application | It is used for call the component to preview on browser.. |
Live Demo
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
Create Parent Component
Step 1:- Create Lightning Web Component : parentCmpLwc.html
SFDX:Lightning Web Component >> New >> parentCmpLwc.html
parentCmpLwc.html [Lightning Web Component HTML]
<template>
<lightning-card>
<h3 slot="title">
<lightning-icon icon-name="utility:connected_apps" size="small"></lightning-icon> Pass the multiple values from parent component to child component in LWC
</h3>
<div class="slds slds-p-horizontal--medium">
<lightning-icon icon-name="utility:down" size="x-small"> </lightning-icon> <span style="color:#ff0000; display:inline-block; margin-left:5px;">Parent Component</span>
<div class="slds-grid slds-wrap">
<div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom--small"><lightning-input type="text" label="User Name" class="userName"></lightning-input></div>
<div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom--small"><lightning-input type="text" label="Email" class="userEmail"></lightning-input></div>
<div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom--small"><lightning-input type="text" label="Phone" class="userPhone"></lightning-input></div>
<div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom--small"><lightning-input type="text" label="Address" class="userAddress"></lightning-input></div>
<div class="slds-col slds-size_12-of-12 slds-p-horizontal--medium slds-text-align--center" >
<lightning-button label="Action Method" variant="brand" size="small" onclick={handleAction}></lightning-button>
</div>
</div>
<lightning-icon icon-name="utility:down" size="x-small"></lightning-icon> <span style="color:#ff0000; display:inline-block; margin-left:5px;">Child Component</span>
<c-child-cmp-lwc></c-child-cmp-lwc>
</div>
<br/><br/>
<!--Start RelatedTopics Section-->
<div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
<p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/05/thumbsUpLike.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:16px; font-style:italic; display:inline-block; margin-right:5px;">Don't forget to check out:-</span><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer" style="text-decoration:none;" data-aura-rendered-by="440:0">An easy way to learn step-by-step online free Salesforce tutorial, To know more Click <span style="color:#ff8000; font-size:18px;" data-aura-rendered-by="442:0">Here..</span></a></strong></p>
<br/><br/>
<p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/07/tickMarkIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:17px; font-style:italic; display:inline-block; margin-right:5px; color:rgb(255 128 0);">You May Also Like →</span> </strong></p>
<div style="display:block; overflow:hidden;">
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<li><a href="https://www.w3web.net/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li>
<li><a href="https://www.w3web.net/display-account-related-contacts-in-lwc/" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li>
<li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li>
<li><a href="https://www.w3web.net/if-and-else-condition-in-lwc/" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li>
<li><a href="https://www.w3web.net/get-selected-radio-button-value-and-checked-default-in-lwc/" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li>
</ul>
</div>
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<li><a href="https://www.w3web.net/display-account-related-contacts-lwc/" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li>
<li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li>
<li><a href="https://www.w3web.net/fetch-picklist-values-dynamic-in-lwc/" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li>
<li><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li>
<li><a href="https://www.w3web.net/update-parent-object-from-child/" target="_blank" rel="noopener noreferrer">update parent field from child using apex trigger</a></li>
</ul>
</div>
<div style="clear:both;"></div>
<br/>
<div class="youtubeIcon">
<a href="https://www.youtube.com/channel/UCW62gTen2zniILj9xE6LmOg" target="_blank" rel="noopener noreferrer"><img src="https://www.w3web.net/wp-content/uploads/2021/11/youtubeIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/> <strong>TechW3web:-</strong> To know more, Use this <span style="color: #ff8000; font-weight: bold;">Link</span> </a>
</div>
</div>
</div>
<!--End RelatedTopics Section-->
</lightning-card>
</template>
Step 2:- Create Lightning Web Component : parentCmpLwc.js
SFDX:Lightning Web Component >> New >> parentCmpLwc.js
parentCmpLwc.js [LWC JavaScript File]
import { api, LightningElement } from 'lwc';
export default class ParentCmpLwc extends LightningElement {
@api username;
@api useremail;
@api userphone;
@api useraddress;
handleAction(){
this.username = this.template.querySelector('.userName').value;
this.useremail= this.template.querySelector('.userEmail').value;
this.userphone= this.template.querySelector('.userPhone').value;
this.useraddress= this.template.querySelector('.userAddress').value;
var fullValueStr = {
getUserName:this.username,
getUserEmail:this.useremail,
geUserPhone:this.userphone,
getUserAddress:this.useraddress
};
window.console.log(JSON.stringify('fullValueStr' + fullValueStr));
this.template.querySelector("c-child-cmp-lwc").displayChildValueAction(fullValueStr);
this.username = this.template.querySelector('.userName').value='';
this.useremail= this.template.querySelector('.userEmail').value='';
this.userphone= this.template.querySelector('.userPhone').value='';
this.useraddress= this.template.querySelector('.userAddress').value='';
}
}
Step 3:- Create Lightning Web Component : parentCmpLwc.js-meta.xml
SFDX:Lightning Web Component >> New >> parentCmpLwc.js-meta.xml
parentCmpLwc.js-meta.xml [LWC Meta Data XML]
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>45.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Create Child Component
Step 4:- Create Lightning Web Component : childCmpLwc.html
SFDX:Lightning Web Component >> New >> childCmpLwc.html
childCmpLwc.html [Lightning Web Component HTML]
<template>
<lightning-card>
<table class="slds-table slds-table_cell-buffer slds-table_bordered slds-table_col-bordered" border="1" cellspacing="0" cellpadding="0" bordercolor="#ccc" style="border-collapse:collapse;">
<thead>
<tr>
<th>User Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr>
<td>{userNameVal}</td>
<td>{userEmailVal}</td>
<td>{userPhoneVal}</td>
<td>{userAddressVal}</td>
</tr>
</tbody>
</table>
</lightning-card>
</template>
Step 5:- Create Lightning Web Component : childCmpLwc.js
SFDX:Lightning Web Component >> New >> childCmpLwc.js
childCmpLwc.js [LWC JavaScript File]
import { api, LightningElement } from 'lwc';
export default class ChildCmpLwc extends LightningElement {
@api userNameVal;
@api userEmailVal;
@api userPhoneVal;
@api userAddressVal;
@api
displayChildValueAction(namestr){
this.userNameVal = namestr.getUserName;
this.userEmailVal = namestr.getUserEmail;
this.userPhoneVal = namestr.geUserPhone;
this.userAddressVal = namestr.getUserAddress;
}
}
Step 6:- Create Lightning Web Component : parentCmpLwc.js-meta.xml
SFDX:Lightning Web Component >> New >> parentCmpLwc.js-meta.xml
parentCmpLwc.js-meta.xml [LWC Meta Data XML]
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>48.0</apiVersion>
<isExposed>false</isExposed>
</LightningComponentBundle>
Create Lightning Application
Step 7:- Create Lightning Application : slwcApp.app
From Developer Console >> File >> New >> Lightning Application
lwcApp.app [Component Application File]
<aura:application extends="force:slds">
<c:parentCmpLwc/>
</aura:application>
Further post that would you like to learn in Salesforce
How do you pass value from parent to child component in lightning component?
There could be multiple solutions for this, I will explain here how you can use 'aura:attribute' and 'Application event' to pass the data from Parent component to Child component. In below example I am passing the variable to the child component using the aura:attribute on the button's click of the Parent Component.
How do you call a child component method from parent in react?
Create a boolean variable in the state in the parent class. Update this when you want to call a function. Create a prop variable and assign the boolean variable. From the child component access that variable using props and execute the method you want by having an if condition.
How does a component communicate down the component hierarchy?
Tip To communicate down the component containment hierarchy, pass properties to the child via HTML attributes, or call its public methods. To communicate between components, use Lightning message service or a publish-subcribe utility. Create and dispatch events in a component's JavaScript class.
Related Topics | You May Also Like
Our Free Courses →
👉 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 |