๐Ÿ”— Parent to Child Communication in LWC Without Message Channels
Sabir Sheikh

Sabir Sheikh @sabir_sheikh_cd49d4c419e3

About: Hi, I'm Mohammad Sabir, a passionate software developer with experience in .NET Core, Flutter, Java, and React JS. I love turning ideas into working code and sharing what I learn along the way.

Joined:
May 4, 2025

๐Ÿ”— Parent to Child Communication in LWC Without Message Channels

Publish Date: May 4
0 0

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

Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘จโ€๐Ÿ‘ฆ 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>

Enter fullscreen mode Exit fullscreen mode

๐Ÿ“„ childComponent.js

import { LightningElement, api } from 'lwc';

export default class ChildComponent extends LightningElement {
    @api textFromParent = '';
}

Enter fullscreen mode Exit fullscreen mode

๐Ÿ“„ 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>

Enter fullscreen mode Exit fullscreen mode

โœ… 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>

Enter fullscreen mode Exit fullscreen mode

๐Ÿ“„ parentComponent.js

import { LightningElement } from 'lwc';

export default class ParentComponent extends LightningElement {
    textInput = '';

    handleChange(event) {
        this.textInput = event.target.value;
    }
}

Enter fullscreen mode Exit fullscreen mode

๐Ÿ“„ 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>

Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก 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** |
| ------------------------------------------------ | ----------------------------- |
| Parent and child are directly connected (nested) | Use
@apiproperty |
| Components are unrelated or not nested | Use Lightning Message Service |
| Child wants to notify Parent | Use Custom Events |

Comments 0 total

    Add comment