@known-as-bmf/store-middleware-persist
TypeScript icon, indicating that this package has built-in type declarations

3.2.6 • Public • Published

@known-as-bmf/store-middleware-persist

Installation

npm install --save @known-as-bmf/store @known-as-bmf/store-middleware-persist

Description

Middleware to be used with @known-as-bmf/store. Allows you to persist state to the browser storage.

Usage

Pass the middleware to of:

import { of } from '@known-as-bmf/store';
import { persistMiddleware } from '@known-as-bmf/store-middleware-persist';

const store = of(
  {
    preferences: { theme: 'dark', lang: 'fr' },
    lastOnline: '2020-02-21T18:22:33.343Z',
    someArray: [],
  },
  persistMiddleware({ storage: localStorage, key: 'uniqueKey' })
);

The state will now be persisted and reloaded on page refresh (depending on the storage method you picked).

API

persistMiddleware

const persistMiddleware = <S>(options?: PersistOptions): Middleware<S>;
interface PersistOptions {
  /**
   * Which `Storage` instance to use for persistence.
   */
  storage?: Storage;
  /**
   * The cache key. Usually a unique key.
   */
  key: string;
}

Package Sidebar

Install

npm i @known-as-bmf/store-middleware-persist

Weekly Downloads

0

Version

3.2.6

License

MIT

Unpacked Size

28 kB

Total Files

20

Last publish

Collaborators

  • known-as-bmf