---
schema_version: "1"
id: e8c9e816e2cce0f9d4d77e047c687f1dd626501624b7223292db93834982cb47
title: Component Event Example
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/aura-platform/guides/aura/events/events-component-example.md
canonical_url: https://developer.salesforce.com/docs/platform/aura-platform/guide/events-component-example.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 2d0673407fc46290861175390162a9d787c94b4b4e0a59f44b5bb696c822f28f
products:
  - Lightning Aura Components Developer Guide
  - Platform Developer Content
---

# Component Event Example

Here’s a simple use case of using a component event to update an attribute in another component.

1.  A user clicks a button in the notifier component, `ceNotifier.cmp`.
2.  The client-side controller for `ceNotifier.cmp` sets a message in a component event and fires the event.
3.  The handler component, `ceHandler.cmp`, contains the notifier component, and handles the fired event.
4.  The client-side controller for `ceHandler.cmp` sets an attribute in `ceHandler.cmp` based on the data sent in the event.

**note:** The event and components in this example use the default `c` namespace. If your org has a namespace, use that namespace instead.

## Component Event

The `ceEvent.evt` component event has one attribute. We’ll use this attribute to pass some data in the event when it’s fired.

<!-- otherprops: set_clipboard -->

```nolang
<!--c:ceEvent-->
<aura:event type="COMPONENT">
    <aura:attribute name="message" type="String"/>
</aura:event>
```

## Notifier Component

The `c:ceNotifier` component uses `aura:registerEvent` to declare that it may fire the component event.

The button in the component contains an `onclick` browser event that is wired to the `fireComponentEvent` action in the client-side controller. The action is invoked when you click the button.

<!-- otherprops: set_clipboard -->

```nolang
<!--c:ceNotifier-->
<aura:component>
    <aura:registerEvent name="cmpEvent" type="c:ceEvent"/>

    <h1>Simple Component Event Sample</h1>
    <p><lightning:button
        label="Click here to fire a component event"
        onclick="{!c.fireComponentEvent}" />
    </p>
</aura:component>
```

The client-side controller gets an instance of the event by calling `cmp.getEvent("cmpEvent")`, where `cmpEvent` matches the value of the name attribute in the `<aura:registerEvent>` tag in the component markup. The controller sets the `message` attribute of the event and fires the event.

<!-- otherprops: set_clipboard -->

```javascript
/* ceNotifierController.js */
{
    fireComponentEvent : function(cmp, event) {
        // Get the component event by using the
        // name value from aura:registerEvent
        var cmpEvent = cmp.getEvent("cmpEvent");
        cmpEvent.setParams({
            "message" : "A component event fired me. " +
            "It all happened so fast. Now, I'm here!" });
        cmpEvent.fire();
    }
}
```

## Handler Component

The `c:ceHandler` handler component contains the `c:ceNotifier` component. The `<aura:handler>` tag uses the same value of the `name` attribute, `cmpEvent`, from the `<aura:registerEvent>` tag in `c:ceNotifier`. This wires up `c:ceHandler` to handle the event bubbled up from `c:ceNotifier`.

When the event is fired, the `handleComponentEvent` action in the client-side controller of the handler component is invoked.

<!-- otherprops: set_clipboard -->

```nolang
<!--c:ceHandler-->
<aura:component>
    <aura:attribute name="messageFromEvent" type="String"/>
    <aura:attribute name="numEvents" type="Integer" default="0"/>

    <!-- Note that name="cmpEvent" in aura:registerEvent
     in ceNotifier.cmp -->
    <aura:handler name="cmpEvent" event="c:ceEvent" action="{!c.handleComponentEvent}"/>

    <!-- handler contains the notifier component -->
    <c:ceNotifier />

    <p>{!v.messageFromEvent}</p>
    <p>Number of events: {!v.numEvents}</p>

</aura:component>
```

The controller retrieves the data sent in the event and uses it to update the `messageFromEvent` attribute in the handler component.

<!-- otherprops: set_clipboard -->

```javascript
/* ceHandlerController.js */
{
    handleComponentEvent : function(cmp, event) {
        var message = event.getParam("message");

        // set the handler attributes based on event data
        cmp.set("v.messageFromEvent", message);
        var numEventsHandled = parseInt(cmp.get("v.numEvents")) + 1;
        cmp.set("v.numEvents", numEventsHandled);
    }
}
```

## Put It All Together

Add the `c:ceHandler` component to a `c:ceHandlerApp` application. Navigate to the application and click the button to fire the component event.

`https://MY_DOMAIN_NAME.lightning.force.com/c/ceHandlerApp.app`.

If you want to access data on the server, you could extend this example to call a server-side controller from the handler’s client-side controller.

**See Also**

- [Component Events](https://developer.salesforce.com/docs/platform/aura-platform/guide/events-component.md)
- [Creating Server-Side Logic with Controllers](https://developer.salesforce.com/docs/platform/aura-platform/controllers-server/controllers-server-intro.md)
- [Application Event Example](https://developer.salesforce.com/docs/platform/aura-platform/guide/events-application-example.md)
