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" ?


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.

| improve this answer | |
  • 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. – the_phantom Mar 12 '18 at 6:33
  • @the_phantom Yes, but again, no guarantee. You just need to wait until the attributes are ready. – sfdcfox Mar 12 '18 at 15:11

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.