Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Lightning - Passing data up the component hierarchy via a component event
<aura:event type="COMPONENT">
<aura:attribute name="param" type="String"/>
</aura:event>
<aura:component>
<aura:handler name="myComponentEvent" event="c:componentEvent" action="{!c.handleMyComponentEvent}"/>
<c:componentEventSender/>
</aura:component>
({
handleMyComponentEvent : function(component, event, helper) {
var value = event.getParam("param");
alert("Received component event with param = "+ value);
}
})
<aura:component>
<aura:registerEvent name="myComponentEvent" type="c:componentEvent"/>
<lightning:button label="Fire component event" onclick="{! c.fireMyComponentEvent }" />
</aura:component>
({
fireMyComponentEvent : function(component, event, helper) {
var myEvent = component.getEvent("myComponentEvent");
myEvent.setParams({"param": "It works!"});
myEvent.fire();
}
})
@khladkyi

This comment has been minimized.

Copy link

@khladkyi khladkyi commented Dec 20, 2017

The example is not working.
A missing piece of code is:
<c:componentEventSender myComponentEvent="{!c.handleMyComponentEvent}" />

@meajinkya

This comment has been minimized.

Copy link

@meajinkya meajinkya commented Jun 13, 2018

@khladkyi It should work. The event handler is already specified in componentEventReceiver.cmp at line #2.

@AjManoj

This comment has been minimized.

Copy link

@AjManoj AjManoj commented Jan 21, 2019

for some reason it doesn't in some situations even i tried,not sure what is the exact cause

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment