Communication between components in LWC
September 20, 2023
Parent To Child
- using @api method
childComp.js
import { LightningElement, track,api } from "lwc";
export default class ChildComp extends LightningElement {
@track value=100; //reactive in nature
//public method
@api changeValue() {
this.value=200;
}
}
childComp.html
<template>
The Value is : {value}
</template>
parentComp.html
<template>
<!--calling child web component here-->
<c-child-comp></c-child-comp>
<lightning-button variant="brand" label="Submit" onclick={handleClick}></lightning-button>
</template>
parentComp.js
import { LightningElement } from "lwc";
export default class ParentComp extends LightningElement {
handleClick() {
//firing an child method
this.template.querySelector("c-child-Comp").changeValue();
}
}
- using @api variable
childComp.js
import { LightningElement, track,api } from "lwc";
export default class ChildComp extends LightningElement {
//public property
@api name;
connectedCallback(){
console.log('data from parent is::'+this.name);
}
}
childComp.html
<template>
The Value from parent is : {name}
</template>
parentComp.html
<template>
<!--calling child web component here-->
<c-child-comp name={dataFromParent}></c-child-comp>
<!-- if you write name-from-parent={dataFromParent} then you need to write in the child js like @api nameFromParent; -->
<lightning-button variant="brand" label="Submit" onclick={handleClick}></lightning-button>
</template>
parentComp.js
import { LightningElement } from "lwc";
dataFromParent;
export default class ParentComp extends LightningElement {
handleClick() {
this.dataFromParent=event.target.value;
}
}
Child To Parent
- Using declarative event approach
childComp.html
<template>
<lightning-input type="text" value={dataFromChild} onchange={sendDataToParent} ></lightning-input>
</template>
childComp.js
import { LightningElement, track,api } from "lwc";
export default class ChildComp extends LightningElement {
@track dataFromChild;
sendDataToParent(){
let clickedMeTest = new CustomEvent('senddata', {detail : this.dataFromChild});
this.dispatchEvent(clickedMeTest);
}
}
parentComp.html
<template>
<!--calling child web component here-->
<c-child-comp onsenddata={handleChildData}></c-child-comp>
Data from child comp is :: {dataFromParent}
</template>
parentComp.js
import { LightningElement } from "lwc";
dataFromParent;
export default class ParentComp extends LightningElement {
handleChildData() {
this.dataFromParent=event.detail;
}
}
5
2
votes
Article Rating
Subscribe
Login
0 Comments
Oldest
Newest
Most Voted
Inline Feedbacks
View all comments
Latest Post
About Me
Welcome to an Aimer's weblog :)
Hi! Asif Parvez here, I'm from West Bengal's Howrah. I have extensive experience in Apex, Integration (REST API), LWC, ADMIN and working as Senior Salesforce Developer for Dazeworks Technologies(An iLink Digital Company). I am also a content creator and blogger.
I have three certifications(PD-I, PD-II, Salesforce Associate).
Our Visitor
Our Visitor
0
0
1
1
3
8
Users Today : 1
Users Yesterday : 1
Users Last 7 days : 14
Users Last 30 days : 70
Users This Month : 66
Users This Year : 225
Total Users : 1138
Views Today : 1
Views Yesterday : 2
Views Last 7 days : 16
Views Last 30 days : 113
Views This Month : 108
Views This Year : 346
Total views : 1933
Who's Online : 0
Your IP Address : 216.73.216.134
Server Time : April 30, 2026 2:42 pmPowered By WPS Visitor Counter