I have a Lightning Web Component for use in the Salesforce Mobile App. The LWC is an app that has been built out and is working well, but I wanted to add a Settings screen for the users. To do so, I’ve created a few checkboxes I want the user to be able to check or uncheck (they default to checked). These checkbox values are stored on the user’s device using localStorage.setItem(). I am able to store the data on the device and retrieve it when the app launches. I then place the values from the device storage into variables in the LWC (showModel for example). I have been able to verify that the value of the showModel variable is being correctly set based on the data in the device’s memory when the app launches. However, the lightning-input checkbox is not being rendered unchecked if the value of the showModel variable is false. That is, if a user unchecks the checkbox, closes the app, and then relaunches the app, the checkbox appears checked when the app loads even though showModel is being set to false. If anyone knows how I might be able to fix this, help would be greatly appreciated.
Here is the html for the Settings page (showing only one checkbox at the moment):
<lightning-card lwc:if={showSettings}>
<div class="custom-title" slot="title">
<lightning-button-icon icon-name="utility:back" variant="bare" name="settingsBack" alternative-text="Back" title="Back" label="Back" onclick={handleSettingsBack} size="large"></lightning-button-icon> Custom Settings
</div>
<div class="slds-m-around_medium">
<div class= "custom-title slds-text-title_caps slds-text-color_weak slds-p-top_large">Show Fields</div>
<div class="slds-text-body_regular slds-m-top_small slds-m-bottom_medium">Select which fields to see:</div>
</div>
<div class="slds-m-around_medium">
<lightning-layout class="slds-m-top_medium">
<lightning-layout-item flexibility="auto">
<lightning-input type="checkbox" label="Model Name" name="showModel" value={showModel} checked={showModel} onchange={handleShowModelChange}></lightning-input>
</lightning-layout-item>
</lightning-layout>
</div>
</lightning-card>
And the relevant JS:
export default class AssetInput extends LightningElement{
@track showModel = '';
connectedCallback(){
//A few other things get done first, and then...
this.loadSettings();
}
loadSettings(){
if(localStorage.getItem('showModel') !== null){
this.showModel = localStorage.getItem('showModel');
//I placed an alert for a toast message here to verify this runs, and saw the toast.
//At one point I also made a button I could press to show the value of showModel and when pressing the button immediately after the app loaded, it would display "false" but the checkbox would remain checked
}else{
localStorage.setItem('showModel', true); //this is intended to set the value to true the first time the app runs
}
}
handleShowModelChange(event){
localStorage.setItem('showModel', event.target.checked);
this.showModel = event.target.checked;
}
}
It might also be worth mentioning that I have some settings working. I’m allowing the user to set default values for a couple fields, so I’m storing strings for those fields instead of boolean true/false values like for these checkboxes. The strings are all loaded properly and displayed in their lightning-inputs as expected. I’m doing the same thing to load those values as I am for these, but these checkboxes aren’t cooperating.
To summarize: when the checkbox is interacted with while the app is running, everything works as expected. However, if the checkbox is unchecked and the app is reloaded, the checkbox is always rendered checked instead of unchecked.
