How to read from redux persisted store (localstorage)
I have a redux persist configuration like this:
const persistConfig = {
key: 'persistedStore',
storage: storage,
transforms: [expireIn(expireTime, expirationKey, []), encryptor],
whitelist: ['reducer1','reducer2','reducer3', 'reducer4']
};
So the localStorage key is something like this "persist:persistedStore".
I have tried reading the storage like this:
let user = localStorage.getItem('persist:persistedStore')
, that's gives me something back but
when I do something like: user.reducer1, always returns undefined.
console.log(localStorage.getItem('persist:persistedStore')
, returns the following: data from storage:
{"reducer1":"\"U2FsdGVkX1/PRA7nYWyo2/SVOlbMXs0z50XKvWmhxhUDW+DaQ9FU9jIRnVTSLpwzpS5NCWC+xX3V2TWBNGFzUA==\"","reducer3":"\"U2FsdGVkX183kEvgTl8/hC450C9iyVJh++m2ptQlqbFBskhU2B5l43Hyf2azCeF7xe4KcDqzXoO6Zhe7qQV8aaX+tOpPqQrzhS9hjsD8n1Gkbx5c/g0LfzBXnv+zh88ztZBERj5/p51uEmn/yI5TBvdBdtSoeQNTylFTlkIBJSVJtjovl5uNN3Iqc6Pj0WOJTNMnJaQsT/WjWiuODz2fIXgWpiny4Rj3Fek10m/3RVAn45pPrKlZoTFot8ih5oyz8v0oUBPbOcsa+sxfRTaM0AL7Lxud1mr3KFt5gy/9GDGTxukLfI0Z9jsaLH+0p1wicgUZO0pUgPuiF5KfU+WGLxxCbDheFwFU71j/welwyTvG1k6Jjbrq5/SIV2Oh1hQJlZ5G+ODRRwHyPPh07eq5kntUFwfGhxY+xVqB0EYh04+K5nqoauaQVZeRPHlvt7djiuh61BlycyINWBBYugpzES2UEbT2Q7Dtt65087+ST6/KbAEnTsOaJ+xy1GdyYNobE5XkcUXnF+vfv+Bn/6V9+1wHpnVYTGPBWIJ6xlN+6s4tkNp+nC7bGQ6r8G8boUnqA9ANQHX4w2S+jr7ysTa5udmQ1Ch7YC/Efb++ywdIUWs7tqZpeSngRzwTQ/jtzPcAoKD4zjEWZNCyyjAXa1P/rpv++KcnWyHbw7VfSBd0BXo=\"","reducer2":"\"U2FsdGVkX19nPr02OWgj71q600tUk0PU5hYZVm7G+pAcByNvztMt7C9BqFLCYyOBLzfi0l/UdPntltGPoKEKpguIis2chKHPyX0c2c7owNQ=\""
Since localStorage
persists data in strings, you'll need to stringify the JSON before it gets stored, and then parse it when you retrieve it.
// storage
const storage = { "reducer1": { "someKey": "Some Data" } }
localStorage.setItem('persistedStore', JSON.stringify(storage))
// retrieval
const retrievedStoreStr = localStorage.getItem('persistedStore') // this is a string
const retrievedStore = JSON.parse(retrievedStoreStr) // this will be a JSON object
const reducer1 = retrievedStore.reducer1 // should now have your reducer