For a child component to communicate with parent component, A child component dispatches a custom event that triggers an update in the parent component. While sending an event to parent component, you can choose to send data and you can allow the event to bubble up through DOM. Along with an event, you can send custom information and make use of that information to update something.

If you have used custom lookup component in LWC, this component works on Child to Parent communication, where a user selected record is dispatched as an event to parent component where it is captured and shown as a pill. This is just a small example.
In this blog we will see how to use the custom event method with a small example.
We have 2 components here
1)Parent component
2)Child component
parent.html
<template>
<lightning-card title="Hello this is Papa Cpmponent">
<p class="slds-p-horizontal_small">
<lightning-input type="text" label="Name" value={Name} read-only="true" ></lightning-input>
<lightning-input type="text" label="Actor" value={Actor} read-only="true" ></lightning-input>
<lightning-input type="text" label="Movie" value={Movie} read-only="true" ></lightning-input>
<lightning-input type="text" label="Punchline" value={Punchline} read-only="true" ></lightning-input>
</p>
<div slot="footer">
<div class="slds-grid slds-gutters">
<div class="slds-col">
<c-child-comp child-name="Raju" oncharacterinfo={handle_Details}>
</c-child-comp>
</div>
<div class="slds-col">
<c-child-comp child-name="Vicky" oncharacterinfo={handle_Details}>
</c-child-comp>
</div>
<div class="slds-col">
<c-child-comp child-name="Baburao" oncharacterinfo={handle_Details}>
</c-child-comp >
</div>
</div>
</div>
</lightning-card>
</template>
parent.js
import { LightningElement } from 'lwc';
export default class Papa extends LightningElement {
Name;
Actor;
Movie;
Punchline;
handle_Details(event){
console.log(JSON.stringify(event.detail));
this.Name = event.detail.a_Name;
this.Actor = event.detail.a_Actor;
this.Movie = event.detail.a_Movie;
this.Punchline = event.detail.a_Punchline;
console.log ('-=-= name: '+this.Name);
}
}
If you observe papa.html file. I have used a lightning card, with body and footer.
The body part will only display details that we receive from child component
Footer part has child components which is used 3 times representing 3 different movie character.
childComp.html
<template>
<lightning-card title={childName}>
<lightning-button
variant="brand"
label="Check Details"
onclick={handleClick}
></lightning-button>
</lightning-card>
</template>
childComp.js
import { LightningElement,api } from 'lwc';
export default class ChildComp extends LightningElement {
@api childName = '';
Name = '';
Actor = '';
Movie = '';
Punchline = '';
handleClick(event){
console.log('child click event: '+event);
switch (this.childName) {
case 'Raju':
this.Name = 'Raju';
this.Actor = 'Akshay Kumar';
this.Movie = 'Hera Pheri';
this.Punchline = 'Paisa hi paisa hoga!';
break;
case 'Vicky':
this.Name = 'Vicky Chopra';
this.Actor = 'Amitab Bachchan';
this.Movie = 'Sharabi';
this.Punchline = 'Mooche Ho Toh Nathulal Ji Jaise Ho... Varna na ho';
break;
case 'Baburao':
this.Name = 'Baburao Ganpatrao Apte';
this.Actor = 'Phir Paresh Rawal';
this.Movie = 'Hera Pheri';
this.Punchline = 'Uthala le re baba';
break;
default:
break;
}
console.log('-=-= in child: '+this.Name);
console.log('-=-= in child: '+this.Movie);
console.log('-=-= in child: '+this.Actor);
console.log('-=-= in child: '+this.Punchline);
const a_Name = this.Name;
const a_Movie = this.Movie;
const a_Actor = this.Actor;
const a_Punchline = this.Punchline;
const selectedCharacter = new CustomEvent('characterinfo', {
detail: {
a_Name,
a_Movie,
a_Actor,
a_Punchline
},
});
this.dispatchEvent(selectedCharacter);
}
}
If you see child component’s html part. I have just added a button to click and get details.
In childComp.js file, button click event, Based on name of the child component, details will be fetched using switch statement.
This is the place where you can add custom code and make some server calls to get any other details as per requirement.
I have used a constant called selectedcharacter which is a custom event.
const selectedCharacter = new CustomEvent('characterinfo', {
detail: {
a_Name,
a_Movie,
a_Actor,
a_Punchline
},
});
this.dispatchEvent(selectedCharacter);
here, if you observe ‘characterinfo’ that is the event name, you can replace it with any character that you want. Any name that you use in child component, in parent you must have an on as an event
If you observe how the component is called in parent component,
<c-child-comp child-name="Raju" oncharacterinfo={handle_Details}>
</c-child-comp>
name of the component is saved as childComp. while using the component in a parent component, there will be no CAPITAL case for any character, if there is a Capital case, it will be replaced with -<small case of same character>. In above example, childComp, here C is capital so while using in parent compnent, we have to mention -c. that is the reason we have used <c-child-comp >
oncharacterinfo ={handle_Details} is used for custom event details. If you use custom event as moviecast, then in parent component you must use onmoviecast as event name and call a method to handle that event.
If you observe the child component, you see detail as a variable, this is a event property, you can also use details as event property. Please note, you cannot use any other variable, you have to be specific and it should be one of the property of event.
Output





