Closed GabotronES closed 5 years ago
Sorry this is a little off-topic, but why is storing data to a cookie better than local storage? I read that in the docs and can't understand why it's an advantage?
@mattharley Cookies have a time to live (defaults to duration of browser session)
@mattharley Cookies have a time to live (defaults to duration of browser session)
Oh thanks for that. Makes sense
I am facing the same problem, it is not saving the state
to cookies tab. Is there any way to fix this issue?
Cookie-JS does not adhere to the interface we are used to with localstorage and the like , therefore a customsaveState
and restoreState
function is needed.
Also do not write your own reducer when using the modules and filter props.
for restoreState
you have the option between.get(
) and .getJSON()
, the latter will JSON.parse()
the retrieved string for you if you were storing a stringified object like in the example below.
import Cookie from 'js-cookie
const vuexSession = new VuexPersist({
key: 'knuckle-user',
storage: Cookie,
modules: ['user'],
filter: mutation => (
mutation.type !== 'user/_walletExists'
),
restoreState: (key, storage) => {
let item = Cookie.getJSON(key)
if (item) {
item.user.wallet = Wallet.fromMnemonic(item.user.wallet.signingKey.mnemonic, `m/99'/66'/0'/0/0`)
return item
}
},
saveState: (key, state, storage) => state ? Cookie.set(key, JSON.stringify(state)) : ''
})
@kyriediculous thanks for answering this question?
I'm using the plugin for a cookie consent modal, I'm using namespaced modules on my store, however my state is not persisiting and after accepting cookies I inmediately refresh the tab but modal is still there.
This is my code:
And the shape of my state: