Skip to content

rememberEnhancer

A Redux enhancer that handles automatic state persistence and rehydration.

rememberEnhancer(
driver: Driver,
rememberedKeys: string[],
options?: Options
): StoreEnhancer

See Driver and Options type definitions.

  • Type: Driver
  • Description: Storage driver instance that implements setItem(key, value) and getItem(key)

Built-in Options:

  • window.localStorage - Persistent storage (web)
  • window.sessionStorage - Session storage (web)
  • AsyncStorage - Async storage (React Native)
  • Custom driver - Any object implementing the Driver interface

Driver Interface:

interface Driver {
setItem(key: string, value: any): void | Promise<void>;
getItem(key: string): any | Promise<any>;
}
  • Type: string[]
  • Description: Array of state keys to persist
  • Note: If an empty array is provided, nothing will be persisted
  • Type: Options
  • Description: Configuration object for customizing behavior

Options Interface: (see Options)

interface Options {
prefix?: string;
serialize?: (state: any, key: string) => string;
unserialize?: (state: string, key: string) => any;
persistThrottle?: number;
persistDebounce?: number;
persistWholeStore?: boolean;
errorHandler?: (error: PersistError | RehydrateError) => void;
initActionType?: string;
}
  • Type: string
  • Default: '@@remember-'
  • Description: Prefix for storage keys
  • Type: (state: any, key: string) => string
  • Default: JSON.stringify
  • Description: Function to serialize state before persisting
  • Type: (state: string, key: string) => any
  • Default: JSON.parse
  • Description: Function to deserialize persisted state
  • Type: number (milliseconds)
  • Default: 100
  • Description: Throttle persistence to limit write frequency
  • Note: Ignored if persistDebounce is provided
  • Best for: Most apps/websites, guarantees your store is persisted on regular intervals
  • Type: number (milliseconds)
  • Default: undefined
  • Description: Debounce persistence (trailing-edge only)
  • Note: If provided, persistThrottle is ignored
  • Best for: Apps/websites where you can guarantee there is some time between store writes and you could tollerate some data loss

Example:

// Debounce for form input
rememberEnhancer(window.localStorage, rememberedKeys, {
persistDebounce: 500 // Wait 500ms after user stops typing
})
// Throttle for real-time updates
rememberEnhancer(window.localStorage, rememberedKeys, {
persistThrottle: 200 // Save at most once every 200ms
})
  • Type: boolean
  • Default: false
  • Description: Whether to persist the entire store as a single item
  • Storage Limits:
    • localStorage: ~5-10MB (varies by browser)
    • sessionStorage: ~5-10MB (varies by browser)
    • AsyncStorage: ~6MB on Android, larger on iOS
  • Warnings:
    • Only use with storage drivers that have large enough limits for your use-case
    • When persisting large states, consider using individual key persistence (default behavior) to avoid quota errors
    • Always implement error handling to catch quota exceeded errors
  • Note: When true, the key parameter is not passed to serialize/unserialize functions
  • Type: (error: PersistError|RehydrateError) => void
  • Default: console.warn
  • Description: Hook function to handle persistence and rehydration errors
  • Note: Error objects include full stack traces
  • See: Error Handling Guide for complete examples
  • Type: string
  • Default: undefined
  • Description: Action type to wait for before initializing
  • Use Case: When you need to do something before state rehydration (e.g., SSR preloading)
  • Note: Redux Remember will be completely disabled until an action with this type is dispatched
  • Type: StoreEnhancer
  • Description: A Redux enhancer to be used with your store