0

I am creating component dynamically with cmp calling it like :

<parent component>
//Child Component dynamic creation
    <aura:attribute type="Aura.Component[]" name="fAttr" />
            {! v.fAttr}
</parent component>

and in the js creating the instance to :

$A.createComponent(
            'c:Footer', {
             "aura:id": "fCrt",
             isVFPage: "True",
            },
            function(newButton, status, errorMessage){
                //Add the new button to the body array
                if (status === "SUCCESS") {
                    var firstPanel = cmp.get("v.fAttr");

                    newButton.set("v.saveFunction", cmp.mymethod); 
                    firstPanel.push(newButton);
                    cmp.set("v.footerAttr", firstPanel);
                }

As everytime on button click the instance is creating a new component , I need to destroy it else it will create new.

1

The framework automatically destroys the old instance once unset from the attribute. There is no need to take additional action.

cmp.set("v.fAttr", [newButton]);

The documentation states you only need to manually destroy components if you never assign them to an attribute of the type Aura.Component[].

After a component that is declared in markup is no longer in use, the framework automatically destroys it and frees up its memory.

If you create a component dynamically in JavaScript and that component isn't added to a facet (v.body or another attribute of type Aura.Component[]), you have to destroy it manually using Component.destroy() to avoid memory leaks.

|improve this answer|||||
  • Right now when somebody moves back to the main parent component and click the button again it create another without destroying the previous one causing another button at a time. I cannot even have the Ids to destroy it . Is there any way like cmp.set("v.fAttr", to destroy and create new one ? – AvengerSF Nov 18 '19 at 20:47
  • @sfdc-neta your code is adding the new button to an stay of the old buttons. Instead, remove the old button entirely. My code is a demonstration of this. – sfdcfox Nov 18 '19 at 22:31

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.