I've read through most of the questions on this issue of ability to remove an item dynamically from DOM, and did understand that this might not be possible.

So I would like to extend the question to query for, if there is a better way available performance wise, to remove a dynamically added child lightning component from parent components' DOM?

Currently, I've 2 components:

  1. Parent CMP
  2. List of Child CMPs

The Child Components are added to Parent Component body dynamically by putting a Component.Aura[] type attribute in component. This attribute would be a list or array of components added. Now, if I want to remove/delete a component at index 2, from the total list of components say 20, the full DOM seems to be regenerated, if I simply do:

var items = component.get("v.items");
items.slice(index, 1);
component.set("v.items", items);

This I think is happening because I am resetting the attribute with array variable after removing one from the list.

This seems to be inducing a significant delay in browser being frozen for few seconds, when the list has like 15-20 items in display. I was thinking if there is a better and faster way to simply remove the item I've identified to be removed from DOM?

  • FYI I'm using the technique you list code for and don't see a significant delay: perhaps my components are less complex than yours. – Keith C May 2 '17 at 16:32
  • Yes, the individual List item in my situation has kind of complex markup, with multiple <aura:if> rendering. – VarunC May 2 '17 at 17:05
  • I suffered a lot of pain related to the DOM getting inconsistent (see salesforce.stackexchange.com/questions/171143/…) so personally would be loath to interfere in the process. – Keith C May 2 '17 at 17:15
  • That is a good point Keith. Though in my case, mine child component is contained in singular container. – VarunC May 2 '17 at 17:25
  • My point was more that the Lightning code can get lost walking around the DOM through bad component markup so may also be fragile if the DOM is modified without its knowledge. – Keith C May 2 '17 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.