@composi/datastore:
putInLocalStorage
Saving to localStorage
Sometimes you may need to persist a dataStore state across user sessions. You can do this using putInLocalStorage
. This saves the state of the dataStore in localStorage. putInLocalStorage
returns a promise, so it is non-blocking. You can use it in a watcher to automatically save the dataStore state when it changes. You would want to do this to preserve the state when a user adds or deletes items from a list, etc.
// We just put a callback in the watcher
// because we're using the default event:
dataStore.watch(() => {
// When state changes, save to localStorage.
dataStore.putInLocalStorage()
})
After you've saved a dataStore's state in localStorage, you can retrieve it during a page load to hydrate a dataStore with it. Read the docs for getFromLocalStorage to learn how.