In Lightning Web Components (LWC), when your child component is nested inside the parent, you can easily pass data using @api decorated properties. This is the simplest and most direct way to enable communication from parent to child β no need to use Lightning Message Service or Custom Events.
π― Use Case
We want to:
- Create a parent component with a textbox.
- As the user types into the textbox, the child component should receive and display the text in real time.
- The child component is embedded inside the parent.
π Final Output
πΉ As you type something in the parent input field, it updates the child display area instantly.
π Folder Structure
lwc/
βββ parentComponent/
β   βββ parentComponent.html
β   βββ parentComponent.js
β   βββ parentComponent.js-meta.xml
βββ childComponent/
    βββ childComponent.html
    βββ childComponent.js
    βββ childComponent.js-meta.xml
π¨βπ¦ Parent-to-Child Communication
β
 childComponent
π childComponent.html
<template>
    <lightning-card title="Child Component">
        <p class="slds-p-around_medium">
            Text from Parent: <strong>{textFromParent}</strong>
        </p>
    </lightning-card>
</template>
π childComponent.js
import { LightningElement, api } from 'lwc';
export default class ChildComponent extends LightningElement {
    @api textFromParent = '';
}
π childComponent.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>59.0</apiVersion>
      <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle>
β
 parentComponent
π parentComponent.html
<template>
    <lightning-card title="Parent Component">
        <lightning-input 
            label="Enter Text" 
            value={textInput} 
            onchange={handleChange}>
        </lightning-input>
        <c-child-component text-from-parent={textInput}></c-child-component>
    </lightning-card>
</template>
π parentComponent.js
import { LightningElement } from 'lwc';
export default class ParentComponent extends LightningElement {
    textInput = '';
    handleChange(event) {
        this.textInput = event.target.value;
    }
}
π parentComponent.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>59.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__HomePage</target>
        <target>lightning__RecordPage</target>
    </targets>
</LightningComponentBundle>
π‘ Explanation
- The parent component owns the state (textInput).
- It listens for changes via onchange.
- It passes the data to the child component through the attribute text-from-parent.
- The child receives that value via a public property decorated with @api.
π§ Key Takeaways
- Use @api in the child to create a public property.
- Use the property in parent HTML like a normal attribute.
- Ideal when your components are nested and related.
π When to Use This Pattern
| **Scenario**                                     | **Solution**                  |@api
| ------------------------------------------------ | ----------------------------- |
| Parent and child are directly connected (nested) | Useproperty           |
| Components are unrelated or not nested           | Use Lightning Message Service |
| Child wants to notify Parent                     | Use Custom Events             |
 

 
    
Top comments (0)