redux-persist-memory-storage

    0.4.0 • Public • Published

    redux-persist-memory-storage npm version

    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:

    1. As a fallback when trying to use localforage or redux-persist-cookie-storage on a client that's blocked access to cookies or localStorage (see this discussion for more)
    2. 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 object
    • logger - optional function called for debugging:
    > const storage = new MemoryStorage({ logger: console.log.bind(console) })
    > storage.setItem('foo', 'bar')
    setItem called with foo bar
    
    

    Install

    npm i redux-persist-memory-storage

    DownloadsWeekly Downloads

    241

    Version

    0.4.0

    License

    MIT

    Unpacked Size

    16 kB

    Total Files

    7

    Last publish

    Collaborators

    • modosc