1

As per SFDC Lightning documentation, when a component is rendered or rerendered, logic can be written in custom renderer to perform post-processing on the DOM.

$A.getCallback() is used to modify a component outside the normal rerendering lifecycle, such as in a setTimeout() call.

What is the difference then when the goal is to modify the DOM? How does it affect whether its inside or outside the normal lifecycle? An explanation with an example will be great!

4

Using setTimeout() to perform an action after rendering would be a hack. You'd have to guess at how much elapsed time it will take the browser to do the rendering, and you won't be able to make that 100% reliable. So, I'd say just don't do it.

The point of $A.getCallback(), as you say, for when things happen outside the Lightning event loop. That may be using setTimeout (for a scenario when you really do want a delay in terms of time), or when another framework needs a callback (e.g. Promises, jquery-ui callbacks, etc.).

The only way to reliably know that a component has been rendered is to use either a custom renderer (on the afterRender and/or rerender events), or handle the render event (new in Summer 17). You should almost never need to do this unless you are making use of a Javascript library. If you're doing something like adding event handlers in a renderer, you'd be better using Lightning to achieve the same results.

On the whole, any direct interaction with the DOM is much more complicated than just working within the components you're given and interacting with them as components rather than elements. So, avoid it unless you really have to.

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.