pure-store offline
multicam opened this issue · 1 comments
multicam commented
Hi there,
Love pure-store --
Giving it a crack in one of my apps --
I'd like to attempt a persistence mechanism for it in the spirit of 'unstated-persist; --
https://github.com/rt2zz/unstated-persist/blob/master/packages/unstated-persist/src/unstated-persist.js
Thoughts and comments would be welcome,
Kind regards
gunn commented
Hi @multicam!
Persistence can potentially be very simple. Here's what I would start with:
import createStore from 'pure-store'
const STORAGE_KEY = "myapp-data"
const storedData = JSON.parse(localStorage.getItem(STORAGE_KEY))
const store = createStore(storedData || getDefaultData())
window.addEventListener("beforeunload", ()=> {
localStorage.setItem(STORAGE_KEY, JSON.stringify(store.state))
})
Depending on your requirements, you might add a more sophisticated storage system like localForage. You might also want to validate the data that's loaded before adding it to the store.