4

is it possible to pass some arguments from the onInit method of a Lightning component to be performed when the renderer finishes?

In my case I'm loading data in the onInit and want to modify some radio buttons' html based on the loaded data. I can't use

cmp.find('foo').getElement()

Because my radio buttons are not yet rendered, I was thinking about puting my loading code in the afterRender, but this seems wrong to me.

I've read, that there is an Event called aura:doneRendering, but it is not recommended to use it, since it can be called multiple times during the components lifecycle.

I was thinking about something like an asynchronous call from [onInit] with an anonymous function to be performed, after

this.superAfterRender();

so I can load my data, and tell the afterRender what I want to do with it.


While writing this, I figured that I can simply store my loaded data in an aura:attribute and use it in the afterRender

So my question is, which of these methods should be used, or is there another one?

  1. Store data from [onInit] in an attribute and retrieve it in afterRender()
  2. Load the data in afterRender()
  3. Use the doneRendering event

1 Answer 1

1

Load your data from doInit, store it on a component attribute

In render &/|| rerender , check if your attribute containing data is not empty, then if not empty do whatever you need to do :)

Afterrender will probably be called before your data is loaded, so it is better to use render & rerender ( probably only rerender ) To avoid doing several times the same rework, you can also ... check if it has been done (analyze DOM or attributes for example) before doing it again

You must log in to answer this question.

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