LWC Communication Part-2 (Passing data from Child to Parent in LWC)

In the last blog, We have seen how to pass data from parent to child lightning web component. Now, there will be scenario where you need to pass data from child to parent Lightning web component.

Before we start, know these points:-

  • To create an event, use the CustomEvent() constructor. To dispatch an event, call the EventTarget.dispatchEvent() method.
  • The CustomEvent() constructor has one required parameter, which is a string indicating the event type. As a component author, you name the event type when you create the event. You can use any string as your event type. However, we recommend that you conform with the DOM event standard.
  • Don’t prefix your event name with the string on, because inline event handler names must start with the string on.
  • Read more here:- Create and Dispatch Events

We have two Lightning Web Component childLwc and parentLwc. Our aim is to pass data from childLwc to parentLwc.

Let’s go through the Code:


We have one input field of type number with onchange event handled in JavaScript file. We have to pass the input value to parent component where childLwc component is called.

    label="Enter a number"


We are getting the value entered by user on change event and passing the data to parent component by creating a custom event with data using detail and dispatching it.

import { LightningElement, api } from "lwc";

export default class ChildLwc extends LightningElement {
  @api progressValue;
  handleChnage(event) {
    this.progressValue = event.target.value;
    // Creates the event with the data.
    const selectedEvent = new CustomEvent("progressvaluechange", {
      detail: this.progressValue

    // Dispatches the event.


<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="childLwc">

We have another component where we are calling the childLwc and handling the event we have created.


We have a progress bar which gets the value from childLwc component. We are handling the event in parentLwc component and getting the data that have been dispatched with custom event.

  <lightning-card title="Getting Data From Child">
    <p class="slds-p-horizontal_small">


We are setting the value received from child to progress bar indicator. event.detail gives the value comes with custom event.

import { LightningElement, track } from "lwc";

export default class ParentLwc extends LightningElement {
  @track progressValue = 0;
  hanldeProgressValueChange(event) {
    this.progressValue = event.detail;


<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="parentLwc">


Deploy both the component to Your org and place it on page to see the results.


Leave a Reply