I have a child component

<aura:component controller="campingController">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    <aura:registerEvent name="add" type="c:enqueueEvent" />
    <lightning:button label="test" onclick="{!c.doInit}"/> 

In its init I am firing a event

doInit : function(component, event, helper) {
        var evt = component.getEvent('add');

Its parent component is handling this event

<aura:component >

    <aura:handler name="add" event="c:enqueueEvent" 
        action="{!c.addInQueue}" includeFacets="true"/>

I am not able to handle event in parent that was fired by child on init.

I know that might be due to reason that Child is initialised first then parent is initialised.

I have gone through Communication between parent and child Lightning components during initialization and Lightning component event not fired or handled but firing application event on init of parent is not a workable solution for me.

do we any other workaround?

  • 1
    I suspect this may be an x-y problem. I suggest you ask a question that directly asks what you're trying to do, instead of focusing on a solution that may not be optimal or even applicable for the problem you're trying to solve. – sfdcfox Feb 19 '18 at 17:40
  • @sfdcfox yes I know I did not provided exactly what problem I am trying to solve. Actually I am currently working on some hobby projects trying to solve problems that I have faced during development. – Manjot Singh Feb 19 '18 at 17:55
  • @sfdcfox thanks for x-y problem article. – Manjot Singh Feb 19 '18 at 17:56
  • @sfdcfox this is what I was working on medium.com/@manjotsingh0074/… – Manjot Singh Feb 21 '18 at 22:04
  • Nifty idea. I haven't had a use for this type of pattern myself, but I'm sure at least a few people have had this problem. – sfdcfox Feb 21 '18 at 22:12

Speaking directly to the question at hand, you should definitely wait for the platform to settle before trying to fire events. As such, if you want an event to go off as soon as the platform's ready, set a callback:

doInit : function(component, event, helper) {
    setTimeout($A.getCallback(() => component.getEvent('add')

This will get picked up immediately after the rendering cycle finishes, and all components in the hierarchy will be able to respond to events.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.