Using Aura method to pass value from Child component to Parent component (with example)

By | February 12, 2021

In this blog, we will discuss how to use <aura:method> to define a method as part of a component’s API. This enables you to directly call a method in a component’s client-side controller instead of firing and handling a component event.

The <aura:method> tag has following attributes:

AttributeTypeDescription
nameStringThe method name. In other words, you can use the method name to call the method in JavaScript code.
actionExpressionThe client-side controller action to execute.
accessStringThe access control for the method. Valid values are:
public—Any component in the same namespace can call the method. This is the default access level.
global—Any component in any namespace can call the method.
descriptionStringThe method description.

An <aura:method> can optionally include parameters. We use an <aura:attribute> tag within an <aura:method> to declare a parameter for the method.

Example

In the below example, we are creating a Parent component aura method with two parameters. These two parameters are ‘GreetingParam’ and ‘PersonNameParam’. After that, we assign these parameters default values of ‘Hello’ and ‘John’ respectively.

ParentComponent

We can define the component using the following code.

<aura:component>
<aura:method name="greetingMethod" action="{!c.getMessage}" access="public">
	<aura:attribute name="GreetingParam" type="String" default="Hello"/> 
	<aura:attribute name="PersonNameParam" type="String" default="John"/> 
</aura:method>
</aura:component>

ParentComponentController

Firstly, in Parent Component controller, we are getting the value of parameters and showing alert.

({
    getMessage : function(component, event) {
        //get method paramaters
        var params = event.getParam('arguments');
        if (params) {
            var param1 = params.GreetingParam;
            var param2 = params.PersonNameParam;
            alert(param1 + " " + param2);
        }
    }
})

ChildComponent

Now in child component we are declaring aura:attribute of type Aura.Component, so we can store parent component reference. Also added button for calling child component controller method.

<aura:attribute name="parent" type="Aura.Component"/>
<lightning:button variant="brand" label="Invoke Parent Method" onclick="{!c.callParentMethod}" />

ChildComponentController

Similarly, in ChildComponentController, we have to get the parent component and calling it’s aura method.

({
	callParentMethod : function(component, event, helper) {
        //Call Parent aura method
        var parentComponent = component.get("v.parent");                         
		parentComponent.greetingMethod()
    }
})

Updated ParentComponent

After that, in the Parent component, we need to add child component and pass the reference in it.

<aura:component>
<aura:method name="greetingMethod" action="{!c.getMessage}" access="public">
	<aura:attribute name="GreetingParam" type="String" default="Hello"/> 
	<aura:attribute name="PersonNameParam" type="String" default="John"/> 
</aura:method>
<c:ChildComponent parent="{!this}"></c:ChildComponent>
</aura:component>

Here is output when users click on Invoke Parent Method button. Output is ‘Hello John’ because no value of parameter is passed. So it is showing default value.

Aura Component Example
Aura Component Example

In the above example, we are using parameters of the parent component. We can also pass parameters from child component to parent component like this:

({
	callParentMethod : function(component, event, helper) {
        //Call Parent aura method
        var parentComponent = component.get("v.parent");                         
		parentComponent.greetingMethod('Happy coding','session')
    }
})

Now when the user click on the Invoke Parent Method button on the child component. The output will be ‘Happy coding session’ instead of ‘Hello John’ because we are passing the value of attributes.

aura:method always executes synchronously so method execution finishes before it returns.

We hope you may find this blog resourceful and helpful. If you still have concerns and need more help, please contact us at salesforce@greytrix.com.

About Us
Greytrix – a globally recognized and one of the oldest Sage Development Partner and a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners and Sage PSG across the globe. We offer Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that require apex code incorporation into the Salesforce platform.

Greytrix has some unique solutions for Cloud CRM such as Salesforce Sage integration for Sage X3Sage 100 and Sage 300 (Sage Accpac). We also offer best-in-class Cloud CRM Salesforce customization and development services along with services such as Salesforce Data MigrationIntegrated App developmentCustom App development and Technical Support to business partners and end users.
Salesforce Cloud CRM integration offered by Greytrix works with Lightning web components and supports standard opportunity workflow. Greytrix GUMU™ integration for Sage ERP – Salesforce is a 5-star rated app listed on Salesforce AppExchange.

The GUMU™ Cloud framework by Greytrix forms the backbone of cloud integrations that are managed in real-time for processing and execution of application programs at the click of a button.

For more information on our Salesforce products and services, contact us at salesforce@greytrix.com. We will be glad to assist you.

Related Posts