EP-15 | Use Custom Events to pass multiple parameters between LWC | 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-15

Parent HTML

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<template>
    <lightning-card title="I am Listening with Parameters" icon-name="utility:user">
        <div class="slds-var-m-around_medium">
            Default Message : {msg} <br/><br/>
        Count of Clicks : {count}
        <c-child-Component-Custom-Eventwith-Data onincreasecount={handleEventChange}>

        </c-child-Component-Custom-Eventwith-Data >
        </div>
    </lightning-card>
</template>

Parent JS

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import { LightningElement } from 'lwc';

export default class ParentComponentCustomEventwithData extends LightningElement {
    endValue = 0;
    count = 1;
    msg = 'Default Message';
    handleEventChange(event) {
        this.endValue = event.detail.endValue;
        this.msg = event.detail.msg;
        if(this.count < this.endValue)
        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
 8
 9
10
11
12
13
14
15
import { LightningElement, api } from 'lwc';

export default class ChildComponentCustomEventwithData extends LightningElement {
    endValue = 5;
    handleOnClick(){
        const myEventwithValue = new CustomEvent('increasecount',{
            detail:{
                endValue : this.endValue,
                msg : "I am String"

            }
        });
        this.dispatchEvent(myEventwithValue);
    }
}


Output



Checkout complete 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