3

All,

This may be a basic question, but I've Googled around it and haven't found an explicit piece of documentation to back it up.

I have a Lightning component with a required attribute which is a list of Custom Object

<aura:attribute name="example" type="Custom_Object__c[]" required="true" default="[]"/>

This component has an init handler which takes the list of Custom objects and effectively turns them into a wrapper class (so I can add a tickbox to each record in the list).

I use this Component [CHILD] in the body of another component [PARENT] (and that component passes in the list of SObjects)

Weirdly, the init on the CHILD component doesn't seem to know about the passed in list of SObjects. It thinks it has an empty list, so it has nothing to iterate through. This surprised me as I had assumed that all of the passed in attributes would be available to the init method.

Is there documentation anywhere which explains the order in which the init handler and the passed in attributes, etc are initialised/kicked-off?

With thanks, Andy

1 Answer 1

8

init event of the child component will called long before the parent's. Add aura:doneRendering event in the child component and access the list in in the child. Order in which init fired is mentioned in the developer guide and in this article too.

When the component tree is ready, the init event is fired for all the components, starting from the children component and finishing in the parent component.

Beware aura:doneRendering will be called multiple times as part of the component lifecycle, when the aura:attributes are dirty. So have a aura:attribute which serves as a flag to help, you decide whether to process the logic.

3
  • Thank you - I had evidently misunderstood that first article and my understanding was completely backwards on orders of nested components, etc. This will be a steep learning curve!
    – Bigears
    Oct 22, 2016 at 9:44
  • @Bigears Even I had the same problem as you had and then I found the above article mentioned in the answer, which helped me to understand how event's work in LC.
    – Praveen
    Oct 22, 2016 at 9:50
  • As of April 2019 the "aura:doneRendering" has been deprecated for most use cases. It is suggested to use the Render event instead. developer.salesforce.com/docs/atlas.en-us.lightning.meta/…
    – rStrunk
    Apr 17, 2019 at 15:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .