@known-as-bmf/store-middleware-persist
v3.2.6
Published
Persistence middleware for @known-as-bmf/store.
Downloads
15
Maintainers
Readme
@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;
}