Building a Simple Data Communication System Between Aura and LWC in Salesforce: A Step-by-Step Guide

In modern application development, flexibility and communication between components are crucial for building dynamic, responsive user interfaces. In Salesforce, developers often work with Aura Components and Lightning Web Components (LWC). While LWCs are more modern and efficient, many Salesforce projects still use Aura Components. If you need to enable communication between these two component types, Salesforce provides several methods to achieve seamless data sharing.

In this blog, I'll guide you through the process of building a simple communication system where data is passed between an Aura component and an LWC. Whether you're just getting started with Salesforce or are an experienced developer, this guide will help you better understand how to integrate Aura and LWC components effectively.


What Are Aura and Lightning Web Components?

Aura Components are Salesforce's original front-end framework, designed for dynamic web apps. Lightning Web Components (LWC), on the other hand, are a more modern framework built on the web standards that provide better performance and a more streamlined development process.

However, many projects still use both Aura and LWC, and being able to pass data between these components is essential for ensuring seamless user experiences.

Scenario Overview

We will build a simple data communication system where:

  1. Aura Component will pass data to LWC.
  2. LWC will send data back to the Aura Component via custom events.

Step-by-Step Guide to Building the Aura-LWC Communication System

1. Define Your Requirements

Before we start coding, let’s clarify the objectives:

  • Aura Component:
    • Accept user input and pass it to the LWC.
    • Display data received from the LWC.
  • LWC:
    • Accept data from the Aura Component.
    • Allow users to send data back to the Aura Component.

2. Set Up Your Aura Component

Now that we have the requirements, let’s create the Aura component.

Aura Component Markup (HTML)
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId" access="global">
    
    <aura:attribute name="dataReceived" type="String"/>
    
    <!-- LWC Communication -->
    <c:lwcCommunication onsenddata="{!c.handleLWCData}" aura:id="lwcComponent"/>

    <lightning:card title="Aura Component" icon-name="standard:custom_apps">
        <div class="slds-p-around_medium">
            <!-- Display data received from LWC -->
            <p>Data received from LWC: <strong>{!v.dataReceived}</strong></p>
            
            <!-- Input for passing data to LWC -->
            <lightning:input aura:id="dataToPass" label="Enter Data to Pass to LWC"/>

            <!-- Buttons to interact with LWC -->
            <div class="slds-m-top_medium button-group">
                <lightning:button label="Pass Data to LWC" onclick="{!c.passDataToLWC}"/>
            </div>
        </div>
    </lightning:card>
</aura:component>
Aura Component Controller (JavaScript)
({
    // Handle data received from LWC via custom event
    handleLWCData: function(component, event, helper) {
        const dataFromLWC = event.getParam('dataToSend');
        component.set("v.dataReceived", dataFromLWC); // Store data from LWC
    },

    // Pass data from Aura component to LWC
    passDataToLWC: function(component, event, helper) {
        const lwcComponent = component.find("lwcComponent");
        const dataToPass = component.find("dataToPass").get("v.value");

        // Call the public method of LWC to pass data
        lwcComponent.receiveData(dataToPass);
    }
});

In the Aura component:

  • handleLWCData listens for a custom event fired by the LWC to receive data.
  • passDataToLWC sends data to the LWC by calling its public method.

3. Create the LWC

Next, we’ll create the LWC that interacts with the Aura component.

LWC HTML Markup 
<template>
    <lightning-card title="LWC Component">
        <!-- Display data received from Aura -->
        <p>Data received from Aura: <strong>{dataReceived}</strong></p>

        <!-- Input to send data to Aura -->
        <lightning-input class="dataToSend" placeholder="Enter text to send to Aura"></lightning-input>

        <!-- Button to trigger the data transfer to Aura -->
        <lightning-button label="Send Data to Aura" onclick={communicateToAura}></lightning-button>
    </lightning-card>
</template>
LWC JavaScript 

import { LightningElement, api, track } from 'lwc';

export default class LwcCommunication extends LightningElement {
    @track dataReceived;

    // Receive data from Aura component via public method
    @api receiveData(data) {
        this.dataReceived = data;
    }

    // Dispatch custom event to send data to Aura
    communicateToAura() {
        const dataToSend = this.template.querySelector(".dataToSend").value;

        // Custom event to send data back to Aura
        const sendDataEvent = new CustomEvent('senddata', {
            detail: { dataToSend }
        });

        this.dispatchEvent(sendDataEvent); // Fire event to Aura component
    }
}

5. Test the Component

  • Step 1: The Aura component accepts user input and passes it to the LWC using the receiveData() method.
  • Step 2: The LWC processes the data and displays it.
  • Step 3: When a user clicks the button in LWC, it fires a custom event with the input data, which the Aura component captures and displays.

Conclusion

In this blog post, we built a simple communication system between Aura and LWC components in Salesforce. This setup demonstrates how easy it is to exchange data between these two component types. By understanding how to send and receive data using events and public methods, you can create more dynamic, interactive applications.

This example is just the beginning! You can build on this foundation to handle more complex use cases, such as passing objects, arrays, or even invoking Apex controllers to process data. Let me know in the comments if you have any questions or need further assistance.

Happy coding!

Comments

Popular posts from this blog

A Step-by-Step Guide to Inserting an Account Record Using Apex in Salesforce Lightning Web Components (LWC)

Integrating Salesforce with Google Drive for File Uploads