[SalesForce] Salesforce Lightning afterRender method throws an error after refresh

I have a main component, which contains two more components. They render instead of each other dynamically, using $A.createComponent(), depending on user actions. Both of these components have <ui:inputDate> elements.

First time the component is loaded, everything is fine, but as soon as I refresh the component I have an error message with the following text:

afterRender threw an error in 'markup://ui:inputDate' : Cannot set
property 'value' of null

Still, after closing the error message, component works fine, as if there is no error at all.

Did someone face such issue or have any suggestions on how to fix it?

Best Answer

I found solution to this issue, so I'll post it in case someone would face the same trouble.

The explanation lies in fact that lightning starts the unrender() method when you refresh the component, and removes all DOM from component. Right after that afterRender() method starts, which, as far as I know, should check whether all component elements are rendered. As the unrender() removed all elements, afterRender() can't find them and throws an error.

The solution was to rewrite afterRender() so it won't start if date inputs are not rendered, I used jQuery for this purpose and method looks like:

enter image description here

Related Topic