In LWC communication how to pass the multiple values from parent component to child component through component JavaScript Method in salesforce lightning web component | how to pass value from parent to child component in lwc

3,188 views


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

how to pass value from parent to child component in lwc -- w3web.net

 

You can download file directly from github by Click Here.

 
 

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!

 

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]

  1.   <template>
  2.     <lightning-card>
  3.  
  4.         <h3 slot="title">
  5.             <lightning-icon icon-name="utility:connected_apps" size="small"></lightning-icon> Pass the multiple values from parent component to child component in LWC       
  6.         </h3>           
  7.          <div class="slds slds-p-horizontal--medium">
  8.             <lightning-icon icon-name="utility:down" size="x-small"> </lightning-icon> <span style="color:#ff0000; display:inline-block; margin-left:5px;">Parent Component</span>    
  9.             <div class="slds-grid slds-wrap">
  10.                 <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>
  11.                 <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>
  12.                 <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>
  13.                 <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>
  14.                 <div class="slds-col slds-size_12-of-12 slds-p-horizontal--medium slds-text-align--center" >
  15.                     <lightning-button label="Action Method" variant="brand" size="small" onclick={handleAction}></lightning-button>
  16.                 </div>
  17.             </div>
  18.  
  19.             <lightning-icon icon-name="utility:down" size="x-small"></lightning-icon> <span style="color:#ff0000; display:inline-block; margin-left:5px;">Child Component</span>    
  20.              <c-child-cmp-lwc></c-child-cmp-lwc>
  21.         </div>
  22.  
  23.  
  24. <br/><br/>
  25.    <!--Start RelatedTopics Section-->
  26. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  27.  
  28.             <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>
  29.  
  30.             <br/><br/>
  31.             <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>
  32.             <div style="display:block; overflow:hidden;"> 
  33.                 <div style="width: 50%; float:left; display:inline-block">
  34.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  35.                         <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>
  36.                         <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>
  37.                         <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>
  38.                         <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>
  39.                         <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>
  40.                     </ul>
  41.             </div>
  42.  
  43.             <div style="width: 50%; float:left; display:inline-block">
  44.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  45.                         <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>
  46.                         <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>
  47.                         <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>
  48.                         <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>
  49.                         <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>
  50.                     </ul>
  51.                 </div>
  52.                <div style="clear:both;"></div> 
  53.                <br/>
  54.                 <div class="youtubeIcon">
  55.                     <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>
  56.                 </div>
  57.     </div>
  58.  
  59. </div>
  60.  
  61.   <!--End RelatedTopics Section-->
  62.  
  63.          </lightning-card>
  64. </template>

Step 2:- Create Lightning Web Component : parentCmpLwc.js

SFDX:Lightning Web Component >> New >> parentCmpLwc.js

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 

parentCmpLwc.js [LWC JavaScript File]

  1.   import { api, LightningElement } from 'lwc';
  2.  
  3.  
  4. export default class ParentCmpLwc extends LightningElement {
  5.     @api username;
  6.     @api useremail;
  7.     @api userphone;
  8.     @api useraddress;
  9.  
  10.     handleAction(){
  11.       this.username = this.template.querySelector('.userName').value;
  12.       this.useremail= this.template.querySelector('.userEmail').value;
  13.       this.userphone= this.template.querySelector('.userPhone').value;
  14.       this.useraddress= this.template.querySelector('.userAddress').value;
  15.  
  16.       var fullValueStr = {
  17.          getUserName:this.username,
  18.          getUserEmail:this.useremail, 
  19.          geUserPhone:this.userphone, 
  20.          getUserAddress:this.useraddress
  21.       };
  22.       window.console.log(JSON.stringify('fullValueStr' + fullValueStr));
  23.       this.template.querySelector("c-child-cmp-lwc").displayChildValueAction(fullValueStr);
  24.  
  25.       this.username = this.template.querySelector('.userName').value='';
  26.       this.useremail= this.template.querySelector('.userEmail').value='';
  27.       this.userphone= this.template.querySelector('.userPhone').value='';
  28.       this.useraddress= this.template.querySelector('.userAddress').value='';
  29.  
  30.     }
  31. }

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]

  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>

Create Child Component

Step 4:- Create Lightning Web Component : childCmpLwc.html

SFDX:Lightning Web Component >> New >> childCmpLwc.html

childCmpLwc.html [Lightning Web Component HTML]

  1.   <template>
  2.     <lightning-card>    
  3.         <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;">
  4.             <thead>
  5.                 <tr>
  6.                     <th>User Name</th>
  7.                     <th>Email</th>
  8.                     <th>Phone</th>
  9.                     <th>Address</th>                    
  10.                 </tr>
  11.             </thead>
  12.             <tbody>
  13.                 <tr>
  14.                     <td>{userNameVal}</td>
  15.                     <td>{userEmailVal}</td>
  16.                     <td>{userPhoneVal}</td>
  17.                     <td>{userAddressVal}</td>                   
  18.                 </tr>
  19.             </tbody>
  20.         </table>       
  21.     </lightning-card>
  22. </template>

Step 5:- Create Lightning Web Component : childCmpLwc.js

SFDX:Lightning Web Component >> New >> childCmpLwc.js

childCmpLwc.js [LWC JavaScript File]

  1.   import { api, LightningElement } from 'lwc';
  2.  
  3. export default class ChildCmpLwc extends LightningElement {
  4.     @api userNameVal;
  5.     @api userEmailVal;
  6.     @api userPhoneVal;
  7.     @api userAddressVal;
  8.  
  9.  
  10.     @api
  11.     displayChildValueAction(namestr){
  12.        this.userNameVal = namestr.getUserName;
  13.        this.userEmailVal = namestr.getUserEmail;
  14.        this.userPhoneVal = namestr.geUserPhone;
  15.        this.userAddressVal = namestr.getUserAddress;
  16.     }
  17.  
  18. }

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]

  1.   <?xml version="1.0" encoding="UTF-8"?>
  2. <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
  3.     <apiVersion>48.0</apiVersion>
  4.     <isExposed>false</isExposed>
  5. </LightningComponentBundle>

Create Lightning Application

Step 7:- Create Lightning Application : slwcApp.app

From Developer Console >> File >> New >> Lightning Application

lwcApp.app [Component Application File]

  1.   <aura:application extends="force:slds">
  2.   <c:parentCmpLwc/>
  3. </aura:application>

how to pass value from parent to child component in lwc -- w3web.net

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

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

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

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



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

Leave a Comment