What I am trying to able to navigate to a component that is it self.

So example, I have component A that calls child component B and I want to be able to simply reload component B, and calling the init function as if it is loading from scratch.

Calling the doInit again is not the effect I want, and doing a force:navigateToComponent to itself won't work.

Any suggestion?

put on hold as unclear what you're asking by glls, Pranay Jaiswal, battery.cord, Raul, Himanshu Dec 11 at 10:45

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • You can destroy and recreate the component using component.destroy() and createComponent: jitendrazaa.com/blog/salesforce/… – manjit5190 Nov 30 at 18:35
  • its kind of hard to tell what exactly you are doing - normally, you would want to track the 'state' of your component in an attribute, and be able to reset the state of the component. – glls Nov 30 at 18:35
  • @manjit5190, if I were to use the example in that article, after I close the modal (destory the component), the next thing how do I load the component again without actually click on the open modal button? – MCHam Nov 30 at 19:00
  • @glls, there an example of resetting state of component? – MCHam Nov 30 at 19:01
  • managing states is a generic programming concept - not related to salesforce - you probably want to do some research on the subject – glls Nov 30 at 19:02
up vote 0 down vote accepted

Reload the current component, kind of difficult, because once it destroys itself, its own scripts are gone. The parent can reload it though.

There are various tricks you could use for the parent to reload it... here's one that has worked for me in simple scenarios, not sure if it works when more complex. I'm just using lightning:input as an example but that's the spot your child component would occupy.

In the component:

<aura:attribute name="array" type="Map[]" default="[{}]" />

<aura:iteration items="{!v.array}" var="x">
    <lightning:input name="input1" label="The button will reset this field" />
</aura:iteration>
<lightning:button label="Reset"  onclick="{! c.handleClick }" />

In the controller:

handleClick: function (component, event, helper) {
    component.set("v.array", [{}]);
}

So basically you have an array containing one empty object. You iterate over it, ignoring its value, and you get one copy of your child component. When you click the button, it replaces the array with another one containing another empty object. Because each new object is a new reference, Lightning does not consider it the "same" item. So it destroys the previous copy of the child and instantiates a new one.

Try typing something into the field and then click Reset. You'll notice it blanks out.

With a child component you can have it throw an event up to the parent, and have the parent reset it on demand.

Not the answer you're looking for? Browse other questions tagged or ask your own question.