0

I have been working on a lightning component where I came across a scenario.

My component calls an Apex class on "init" and I also have a custom renderer that does something. However, my custom renderer implementation of afterRender is called before the "init" completes (because the server is taking time to finish) and as a result component attributes are not set. Now, if I try to fetch component attributes values in afterRender, I get undefined for all the attributes that depend on init.

Is there any possible way to make sure that "render" and "afterRender" are only called after "init finishes" and not after "init is started" ?

1 Answer 1

0

Calling $A.enqueueAction all but ensures your render method won't have access to attributes besides those set as default or from a design attribute. render is only to be used to modify the DOM as it will appear, usually by attaching event listeners or custom HTML elements (e.g. a container for a mapping library). If you look at the documentation, you can see that render will be called additional times, so you simply need to wait for the data to be available.

2
  • To me it seems that there is no fixed pattern. afterRender may be called after "init completes" if server returns the response quickly OR it may be called before if server takes time. Thanks for your answer @sfdcfox. Commented Mar 12, 2018 at 6:33
  • @the_phantom Yes, but again, no guarantee. You just need to wait until the attributes are ready.
    – sfdcfox
    Commented Mar 12, 2018 at 15:11

You must log in to answer this question.

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