Component Events with example

louis-reed-wSTCaQpiLtc-unsplash

Components Events

In my previous post, I demonstrated how application events can be used. This post is all about using component events. As explained in my earlier posts, component events are slightly different from application events because they are bound by containment hierarchy. Component events can only be handled by containers (other components) directly enclosing the source of the event. However, there is a way to allow every container in the hierarchy to handle these events. So let’s first see what a regular component event looks like.

<aura:event type=”COMPONENT” description=”Event template”>
<aura:attribute name=”studentID” type=”string”></aura:attribute>
</aura:event>

This is your typical component event with a single payload studentid. It is similar in structure to application events with the only difference being the type. Here type is COMPONENT.

Event Registration

Similar to the application events, component events too, need to be registered by a component before they can fire one. This is similar to application events. Here goes your sample event registration. This registration needs to be done in the markup of the component intending to fire the event.

<aura:registerevent name=”compEvent” type=”c:CompEvent”></aura:registerevent>

This is how a component event can be registered by a firing party.

Leave a reply