Skip to content

Latest commit

 

History

History
58 lines (45 loc) · 2.22 KB

README.md

File metadata and controls

58 lines (45 loc) · 2.22 KB

redux-persist-memory-storage npm version Node.js CI

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