You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I refresh the page the state is lost with unistore which is similar to redux. React/Redux has middleware libs to persist the state. What are the options with unistore ? I found an interesting article on stackoverflow that talks about saving state to local storage. But things can get messy and the article also mentions about performance issues with frequent state changes.
importcreateStorefrom'unistore';// The top-level state properties you want to persistconstKEYS_TO_SAVE=['foo','bar','baz'];conststore=createStore(loadState()||INITIAL_STATE);// throttle savinglettimer;store.subscribe(()=>{if(timer)return;timer=setTimeout(saveState,100);});functionsaveState(){timer=null;letstate=store.getState();letsaved={};for(constkeyofKEYS_TO_SAVE)saved[key]=state[key];localStorage.setItem('state',JSON.stringify(saved));}functionloadState(){try{returnJSON.parse(localStorage.getItem('state'));}catch(e){}}
Activity
developit commentedon Mar 8, 2019
Kanaye commentedon Mar 9, 2019
There is also a library called unissist that provides persistence for unistore using local storage or indexedDB.
developit commentedon Mar 10, 2019
@Kanaye I totally forgot! Definitely use unissist - better to save to IndexedDB for sure.