reactjs - read SessionStorage in redux createState -


i want add sessionstorage next.js/react/redux project in order rid of state loss when refreshing page.
i'm using jwt in order authenticate client api , putting jwt in sessionstorage on login works fine, curios check if there jwt in sessionstorage.
first thought add createstate function in reducer define empty sessiontoken on startup

const createstate = () => ({   // ...   sessiontoken: sessionstorage.token || "",   // ... })  const gui = (state = createstate(), action) => {   switch(action.type) {   // ...   } } 

but i'm getting
sessionstorage not defined.

using
sessiontoken: window.sessionstorage || "",
results in an
window not defined error

ok, thought, i've read wrapping sessionstorage in function in question

so added functions in different file , imported them in reducers, worked fine.
@ least imports.

export const globalstorage = () => {(insnode = typeof process === 'object' && process.versions && !!process.versions.node) ? global : window}  export const setsessionstorage = (key, value) => sessionstorage.setitem(key, value) export const getsessionstorage = (key) => sessionstorage.getitem(key) export const delsessionstorage = (key) => sessionstorage.removeitem(key) 

but when tried call functions in reducer

sessiontoken: globalstorage()['sessionstorage'] || "",

i'm getting:

(0 , _store.globalstorage) not function

following tutorial

my first attempt access sessionstorage directly in createstate function should work fine, doesn't.
missing? :/
appreciated , in advance,
bigz


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -