Do we need multiple lightning events in Salesforce?

Manjot Singh
Salesforce-Lightning
2 min readMar 15, 2018

Lightning framework uses event-driven programming. So while developing Lightning components we work a lot with Lighting events. So do we need to create multiple events to do communication between lightning components? No, we can just do fine with a single component event.

What are problems with having multiple lightning events.

  • If we create a new event for each interaction then there will be a lots of events in our Codebase.
  • You cannot delete your lightning events. If your UI interactions changes and your lightning event become redutant even than you cannot delete it.

So how we can develop our application using only a single event. Lets take example from one of most popular library of lightning components on GitHub Strike-Components. You can look into there code and see that they have used only one component event that is strike_evt.

From where we start. Create a new component Event say state_evt. Note that Attribute type is object so that we can pass any type of data.

I can use this event everywhere in my application. So how i can differentiate between different component events fired. I can do so by registering event with different names.

As you can see i have given different name to same event in childComponent1 and childComponent2 so that parent component can differentiate who fired which event.

Another way to differentiate between different events can be naming events which work with same functionality as same name. Lets say we have a functionality where we are opening a modal. There will be 2 events one will open that modal and another will close it.

Lets say we have added a handler of childEvent in parent component and registered that event in childComponent.

We will send information regarding type of event in data attribute of event.

While handling this event we will use a switch case in parent and then delegate task to helper. ( Delegating task to helper will make your code cleaner. You can add as many case in switch statement.)

In this way you can do as many communication you want to do by using just one component event. If you need application event then you can create one application event also.

--

--