0.0.5 • Published 6 years ago
@micra/store-hooks-persist v0.0.5
@micra/store-hooks-persist
Installation
yarn add @micra/store-hooks-persistUsage
Basic
import { state } from '@micra/store-hooks';
import { persist } from '@micra/store-hooks-persist';
const counter = state<number>(0);
persist({
stores: {
counter,
},
});Remove stored value
import { state } from '@micra/store-hooks';
import { persist } from '@micra/store-hooks-persist';
const counter = state<number>(0);
const remove = persist({
stores: {
counter,
},
});
remove('counter'); // deletes counter value storedSaving part of the store
import { state, computed } from '@micra/store-hooks';
import { persist } from '@micra/store-hooks-persist';
interface Counter {
count: number;
someInfo: string;
}
const counter = state<Counter>({
count: 0,
someInfo: 'that wont be persisted',
});
persist({
stores: {
counter: computed(counter, ({ count }) => ({ count })),
},
});IMPORTANT: the computed value passed to
persist.storesshould be the store's PARTIAL value as this will be merged into the store. If the shape is different, it'll overwrite the store's state and cause bugs.
Custom prefix
Defaults to
$SHP:
import { state } from '@micra/store-hooks';
import { persist } from '@micra/store-hooks-persist';
const counter = state<number>(0);
persist({
prefix: 'MY_PERSISTED_STATE_',
stores: {
counter,
},
});Custom expiration
Defaults to 24h
import { state } from '@micra/store-hooks';
import { persist } from '@micra/store-hooks-persist';
const counter = state<number>(0);
persist({
expiration: 300, // in seconds
stores: {
counter,
},
});Custom storage
Defaults to localStorage
import { state } from '@micra/store-hooks';
import { persist } from '@micra/store-hooks-persist';
const counter = state<number>(0);
persist({
to: sessionStorage,
stores: {
counter,
},
});