EP-14 | Communicate with Custom Events in LWC | Child to Parent Communication | LWC Stack ☁️⚡️


 LWC Stack is Lightning Web Component tutorial series by Salesforce MVP Kapil Batra. In this series you will find LWC tutorials from beginner to intermediate level.

So if you are working on it or planning to learn LWC then this video series is for you. Let's learn and grow together ! Please check complete code below from LWC Stack EP-14


Parent HTML

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<template>
    <lightning-card title="I am Listening" icon-name="utility:user">
        <div class="slds-var-m-around_medium">
        Count of Clicks : {count}
        <c-child-Component-Communication onincreasecount={handleEventChange}>

        </c-child-Component-Communication>
        </div>
    </lightning-card>
</template>
Parent JS
1
2
3
4
5
6
7
8
import { LightningElement } from 'lwc';

export default class ParentComponentCommunication extends LightningElement {
    count =1;
    handleEventChange(){
        this.count = this.count + 1;
    }
}

Child HTML
1
2
3
4
5
6
7
8
9
<template>
    <lightning-card title="I am communicating " icon-name="utility:user">
        <div class="slds-var-m-around_medium">
           <lightning-button label="Count++" onclick={handleOnClick}>
               
           </lightning-button>
        </div>
    </lightning-card>
</template>

Child JS
1
2
3
4
5
6
7
import { LightningElement } from 'lwc';

export default class ChildComponentCommunication extends LightningElement {
  handleOnClick(){
      this.dispatchEvent(new CustomEvent('increasecount'));
  }
}

Output

Checkout complete video tutorial below

 If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks

Happy Coding :)

Post a Comment

0 Comments