Get and Set localStorage variable in Salesforce Lightning Aura Component

Get and Set localStorage variable in Salesforce Lightning Aura Component

localStorage.setItem() and localStorage.getItem() can be used to Get and Set localStorage variable in Salesforce Lightning Aura Component.

Sample Code:

Component:

<aura:component implements="force:appHostable" >
    <aura:handler name="init" value="{!this}" action="{!c.init}"/>
</aura:component>

Component JavaScript Controller:

({
    init : function(component, event, helper) {
        
        localStorage.setItem( 'exampleItem', 'Testing' );
        console.log( 'exampleItem is', localStorage.getItem( 'exampleItem' ) );
        
    }
}) 

Output:

Leave a Reply