Refresh State of Lightning Component after the lightning navigation gets changed.

How to refresh the state of Lightning Component after change of lightning navigation ?




So in my scenario I am having two record types on opportunity and I am rendering the attributes on the component based on the Record Type ID.

For example let's say I am having "RT1" and "RT2". In my doInIt method I was first checking the record type name & Id and based on that I was rendering the attributes.

At first time while creating new opportunity and selecting "RT1", it was displaying "RT1" in my doInIt method. But while I navigate to opportunity again and try creating a new opportunity with "RT2", my doInIt method didn't get triggered as the component was in his old state where the doInIt method was already triggered.

To solve this issue I added lightning:isUrlAddressable in my component as shown below :


1
<aura:component description="testComponent" implements="lightning:isUrlAddressable">


After adding the isUrlAddressable simply add a change handler to refresh the state as shown below :


1
<aura:handler name="change" value="{!v.pageReference}" action="{!c.reInit}" />

Controller.js


1
2
3
 reInit : function(component, event, helper) {
        $A.get('e.force:refreshView').fire();
    }



Now the component state will be refreshed automatically. Please comment below if you would like to update something in this post.




Please check below links also and subscribe if you like the content : 




Stay at Home to Stay Safe. #corona


Share this blog with your friends if you find it helpful somehow !

Thanks
Keep Coding 😊





Post a Comment

0 Comments