{"id":3379,"date":"2020-06-20T03:54:16","date_gmt":"2020-06-20T09:24:16","guid":{"rendered":"https:\/\/www.emizentech.com\/blog\/?p=3379"},"modified":"2022-01-21T11:42:29","modified_gmt":"2022-01-21T11:42:29","slug":"child-to-parent-communication-in-lwc","status":"publish","type":"post","link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/child-to-parent-communication-in-lwc.html","title":{"rendered":"How To Use Child To Parent Communication In LWC In Salesforce"},"content":{"rendered":"<p>In this blog, we will learn the child to parent communication in LWC. We are going to create a progress bar component to understand how a child event can be bubbled up to the parent. We will use the custom event of js to achieve this. To create a custom event we will use CustomEvent() constructor syntax as :<\/p>\n<pre>event = new CustomEvent(typeArg, customEventInit);<\/pre>\n<p>Can check <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/CustomEvent\/CustomEvent\" target=\"_blank\" rel=\"noopener\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/CustomEvent\/CustomEvent<\/a><br \/>\nTo fire the trigger use the dispatchEvent method of EventTarget.<br \/>\nCan check <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/EventTarget\/dispatchEvent\" target=\"_blank\" rel=\"noopener\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/EventTarget\/dispatchEvent<\/a><\/p>\n<h4>Also Read: <a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/properties-in-lightning-web-component.html\" target=\"_blank\" rel=\"noopener\">How To Use Properties In Lightning Web Component<\/a><\/h4>\n<p>Now let\u2019s get your hand dirty with code first and then will understand the flow of code.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"InputProgressBarhtml\"><\/span>InputProgressBar.html<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>&lt;template&gt;\r\n    &lt;lightning-tile label='Child Component'&gt;\r\n        &lt;lightning-input name='barMeter' label='Enter Value for Progress Bar' onchange={progress}&gt;&lt;\/lightning-input&gt;\r\n    &lt;\/lightning-tile&gt;\r\n&lt;\/template&gt;<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"inputProgressBarjs\"><\/span>inputProgressBar.js<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>import { LightningElement } from 'lwc';\r\n\r\nexport default class InputProgressBar extends LightningElement {\r\n    \r\n    progress(event){\r\n        const custEvent = new CustomEvent(\r\n            'callpasstoparent', {\r\n                detail: event.target.value \r\n            });\r\n        this.dispatchEvent(custEvent);\r\n    }\r\n}<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"parentBarCmpjs\"><\/span>parentBarCmp.js<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>import { LightningElement, track } from 'lwc';\r\n\r\nexport default class ParentBarCmp extends LightningElement {\r\n    @track barVal = 10;\r\n    passToParent(event){\r\n        this.barVal = event.detail;\r\n    }\r\n\r\n}<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"parentBarCmphtml\"><\/span>parentBarCmp.html<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>&lt;template&gt;\r\n    &lt;lightning-tile label='Parent Bar Component'&gt;\r\n        &lt;lightning-progress-bar value={barVal} size=\"large\"&gt;&lt;\/lightning-progress-bar&gt;\r\n    &lt;\/lightning-tile&gt;\r\n    &lt;c-input-progress-bar oncallpasstoparent={passToParent}&gt;&lt;\/c-input-progress-bar&gt;\r\n&lt;\/template&gt;<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"parentBarCmpjs-metaxml\"><\/span>parentBarCmp.js-meta.xml<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;LightningComponentBundle xmlns=\"http:\/\/soap.sforce.com\/2006\/04\/metadata\"&gt;\r\n    &lt;apiVersion&gt;48.0&lt;\/apiVersion&gt;\r\n    &lt;isExposed&gt;true&lt;\/isExposed&gt;\r\n    &lt;targets&gt;\r\n        &lt;target&gt;lightning__AppPage&lt;\/target&gt;\r\n        &lt;target&gt;lightning__RecordPage&lt;\/target&gt;\r\n        &lt;target&gt;lightning__HomePage&lt;\/target&gt;\r\n    &lt;\/targets&gt;\r\n&lt;\/LightningComponentBundle&gt;<\/pre>\n<div class=\"center-imgs\"><a href=\"https:\/\/multisitelocal.ezxdemo.com\/contact-us.html?utm_source=blog&amp;utm_medium=banner&amp;utm_campaign=emizen_blog\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-2389 size-full\" src=\" \/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/hire-salesforce-developers-1.png\" alt=\"hire salesforce developers\" width=\"1000\" height=\"200\" \/><\/a><\/div>\n<p>1. First, we have created a child component to receive user input and onchange handler is used to capture the input value.<br \/>\n2. We have created a parent component that is showing a progress bar based on input that the user enters in the child component.<br \/>\n3. So we need to pass this input value from child to parent component and to achieve this we used the event bubbling concept.<br \/>\n4. We have created a custom event and passed that input value and fired that event using dispatchEvent. This way callpasstoparent event fired<\/p>\n<pre>this.dispatchEvent(custEvent);<\/pre>\n<p>5. When this event fired it calls passToParent. In this event, we received the value and set it as progress bar value<\/p>\n<pre>&lt;c-input-progress-bar oncallpasstoparent={passToParent}&gt;&lt;\/c-input-progress-bar&gt;<\/pre>\n<p>This way we are passing value from child to parent component. Using the below syntax can send multiple data as well.<\/p>\n<pre>detail : {\r\n\tparam1: value1,\r\nparam2 : value2\r\n}<\/pre>\n<p>I hope this blog helped you. Keep learning and if you require <a href=\"https:\/\/multisitelocal.ezxdemo.com\/salesforce.html\">salesforce development services<\/a> then get in touch with our <a href=\"https:\/\/multisitelocal.ezxdemo.com\/salesforce-consulting.html\">salesforce consulting<\/a> team.<\/p>\n<h4>Learn more: <a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/create-web-to-lead-web-to-case-forms-in-salesforce.html\" target=\"_blank\" rel=\"noopener\">How To Create Web To Lead \/ Web To Case Forms<\/a> and <a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/handling-form-values-on-event-in-lwc.html\" target=\"_blank\" rel=\"noopener\">How To Handle Form Values On Event In LWC<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will learn the child to parent communication in LWC. We are going to create a progress bar component to understand how a child event can be bubbled up to the parent. We will use the custom event of js to achieve this. To create a custom event we will use CustomEvent()<\/p>\n","protected":false},"author":39,"featured_media":3399,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"MSN_Categories":"Uncategorized","MSN_Publish_Option":false,"MSN_Is_Local_News":false,"MSN_Is_AIAC_Included":"Empty","MSN_Location":"[]","MSN_Add_Feature_Img_On_Top_Of_Post":false,"MSN_Has_Custom_Author":false,"MSN_Custom_Author":"","MSN_Has_Custom_Canonical_Url":false,"MSN_Custom_Canonical_Url":"","_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[87],"tags":[],"class_list":{"0":"post-3379","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-salesforce-development"},"modified_by":"emizentech","featured_image_src":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2020\/06\/Child-To-Parent-Communication-In-LWC-1.jpg","featured_image_src_square":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2020\/06\/Child-To-Parent-Communication-In-LWC-1.jpg","author_info":{"display_name":"Virendra Sharma","author_link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/author\/salesforce"},"_links":{"self":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/3379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/comments?post=3379"}],"version-history":[{"count":0,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/3379\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media\/3399"}],"wp:attachment":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media?parent=3379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/categories?post=3379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/tags?post=3379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}