redux-persist-memory-storage
v0.4.0
Published
memory storage for redux-persist
Downloads
938
Readme
redux-persist-memory-storage
Redux Persist memory storage adapter.
Data isn't persisted anywhere other than memory so this isn't appropriate for general use. There are a few cases where this may be useful:
- As a fallback when trying to use localforage or redux-persist-cookie-storage on a client that's blocked access to
cookies
orlocalStorage
(see this discussion for more) - In testing
Installation
Install with npm:
npm install redux-persist-memory-storage
Usage
Client
Conditionally use redux-persist-cookie-storage:
import { persistStore, autoRehydrate } from 'redux-persist'
import MemoryStorage from 'redux-persist-memory-storage'
import CookieStorage from 'redux-persist-cookie-storage'
const store = createStore(reducer, undefined, autoRehydrate())
persistStore(store, { storage: navigator && navigator.cookieEnabled ? new CookieStorage() : new MemoryStorage() })
Conditionally use localforage
import { persistStore, autoRehydrate } from 'redux-persist'
import MemoryStorage from 'redux-persist-memory-storage'
import localForage from 'localforage'
const store = createStore(reducer, undefined, autoRehydrate())
let enableLocalForage = true
try {
window.localStorage.setItem('__u', 'u')
} catch (e) {
enableLocalForage = false
}
persistStore(store, { storage: enableLocalForage ? localForage : new MemoryStorage() })
Options
new MemoryStorage({ initialState = {}, logger })
initialState
- optional initial state objectlogger
- optional function called for debugging:
> const storage = new MemoryStorage({ logger: console.log.bind(console) })
> storage.setItem('foo', 'bar')
setItem called with foo bar