What are lightning events used for?
Can someone explain to me when I should use the Lightning event when creating Lightning Components?
And kindly give an example thanks.
Kinda confused with the documentation that I'm reading for example Lightning Event in trailhead.
The answer to your - what are lightning events used for is -
- In simple terms, Events are used to establish communication between components. If you want to send the data from one component to another component, we use events.
- You fire an event in the source component and this event will be handled by any other component depending on the type.
- Let's take an example of Account Search. In this scenario, you need to have an input field to enter the search string and you need to display the result in a table.
- Component 1: create an input field and a button to perform a search. Call the apex and get the data. Component 2: Display the result in a table.
- You will get the list of records after clicking on a search button and you need to pass these records to component 2 to display. Here we use Events.
2 types events: Application Events and Component Events
- Application Events are used to establish communication between two or more independent components.
- Component Events are used to establish communication between the components which are in the containment hierarchy.
- Note: Ofcourse, you can do this use case in a single component. I just took this as an example to explain the events.