Interview Questions – Lightning Framework – Part 3 – Component Event

Interview Questions – Lightning Framework – Part 3 – Component Event

Checkout earlier posts in interview question series to read previous questions.

17. What are component events?

Component events allow you to make a communication channel within same hierarchy. A component event is fired from an instance of a component. A component event can be handled by the component that fired the event or by a component in the containment hierarchy that receives the event.

A component event cannot be handled by any component which is below in the component hierarchy or out of the hierarchy.

  • Events fired from c:second component cannot be handled by c:thirdComponent since it is out of component hierarchy. It cannot be handled by c:fourthComponent as well, since it is below c:secondComponent in component hierarchy.

18. What are the steps to fire and handle a component event?

  • Create a component event.
  • Register the component event in the aura component from where you intend to fire it.
  • Fire the component event using fire() method.
  • Declare an aura handler in your component where you intend to handle the event.

19. How can you handle the component event?

Component events can be handled either by aura:handler or directly from child component declaration tag. For example, if c:secondComponent fires a component event named “onBack” than you can handle that directly from c:firstComponent like below:

<c:secondComponent onBack=“{!c.handleBack}” />

20. What are different phases for component event propagation?

The framework supports capture and bubble phases for the propagation of component events. These phases are similar to DOM handling patterns and provide an opportunity for interested components to interact with an event and potentially control the behavior for subsequent handlers.

Read Also – Interview Questions – Lightning Framework – Part 2 – Inheritance

21. What is capture phase in component event propagation?

The event is captured and trickles down from the application root to the source component. The event can be handled by a component in the containment hierarchy that receives the captured event.

Event handlers are invoked in order from the application root down to the source component that fired the event.

Any registered handler in this phase can stop the event from propagating, at which point no more handlers are called in this phase or the bubble phase.

Source component is the one who is firing the event. Application root here is the top-most component in component hierarchy.

22. What is bubble phase in component event propagation?

The component that fired the event can handle it. The event then bubbles up from the source component to the application root. The event can be handled by a component in the containment hierarchy that receives the bubbled event.

Event handlers are invoked in order from the source component that fired the event up to the application root.

Any registered handler in this phase can stop the event from propagating, at which point no more handlers are called in this phase.

Source component is the one who is firing the event. Application root here is the top-most component in component hierarchy.

Read Also – Interview Questions – Lightning Framework – Part 1

23. What is default phase in component event propagation?

Bubble phase is the default phase for component events.

24. How you can stop further propagation of a component event?

stopPropagation() method is used to stop further propagation of the event. This will stop the event at current element itself and it will not traverse the rest of the path.

25. What is the base aura tag for component event?

<aura:event type=“COMPONENT” />

26. How to find source/root of component event?

In the client-side controller action for an  tag, use evt.getSource() to find out which component fired the event, where evt is a reference to the event. To retrieve the source element, use evt.getSource().getElement().

Also Watch : Lightning Component Development Day6 – Firing and Handling Component Events, Commenting Code

 

Read Also:

Manish Choudhari

I am a certified Salesforce Application & System Architect and Developer working on Salesforce Technology since 2014. Currently, I have 14 Salesforce certifications along with OCPJP (Oracle Certified Profession JavaSE6 Programmer) working in Salesforce.com Hyderabad as a Technical Engineer. Writing technical blogs, learning new technologies and frameworks and sharing knowledge is my hobby.

This Post Has 3 Comments

Leave a Reply

Close Menu