Ionic 4 change local storage value

hi, now used local storage to save users information when login

localStorage.setItem(‘name’,res[‘data’][‘name’])

if user edite username it’s not change in app but of close app and open it’s again it’s show
i need change username in all app when change it
i used same code to change name

localStorage.setItem(‘name’,res[‘data’][‘name’])

There’s no good reason to use localStorage for anything. Its weak persistence guarantees make it inappropriate for persisting things between app runs, and using it for in-app communication is slow and not type-safe.

I would suggest using Ionic Storage instead. As for your stale data problem, I use a structure like this.

2 Likes

thank u but u can explain this code in page

It’s not a safe option but if you need to storage data there, just try remove and setting again.

1.- window.localStorage.removeItem(‘name’)
2.- window.localStorage.setItem(‘name’,res[‘data’][‘name’])

IF you absolutely want to save the data using localstorage so some reason use the jwtwebtoken package.

This way the user will be able to see the data on localstorage but making changes to if will corrupt OR pollute the data to be used inside the app.

Also for the question as to “how to change the data in app after the user saves it”
you will need a localstorage.getItem instead of setItem

The reason as to why the name is not changing is because that data is not reactive in itself. depending of the framework you will need to “Watch” that data for a change and then fire a command to update it