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
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
4
4
4
Users Today : 1
Users Yesterday : 6
Users Last 7 days : 142
Users Last 30 days : 282
Users This Month : 156
Users This Year : 531
Total Users : 1444
Views Today : 1
Views Yesterday : 22
Views Last 7 days : 439
Views Last 30 days : 763
Views This Month : 473
Views This Year : 1142
Total views : 2729
Who's Online : 0
Your IP Address : 216.73.216.231
Server Time : June 14, 2026 8:19 pmPowered By WPS Visitor Counter