Make the store persistant
You can easily make your store's any segment to be persisted by persisted
function.
persisted
import { createStorm, persisted } from 'event-storm';
const createPersistedStore = persisted(createStorm)({
storageKey: 'some_store_key',
beforeunload: state => ({
users: state.users,
}),
});
const defaultState = {
users: [],
age: 15,
loading: false,
};
const store = createPersistedStore(defaultState);
IPersistOptions
interface IPersistOptions<T> {
storageKey: string;
beforeunload: (storm: IStormState<T>) => Partial<IStormState<T>>;
permanent?: boolean,
}
Methods
Method | Type | Description |
---|---|---|
beforeunload | (storm: IStormState<T>) => Partial<IStormState<T>> | method is called right before the browser unload event. It will receive the current storm state as an argument. It can return any storm fragment as a return value. The return value will be persisted. |
Properties
Property | Type | Required | Description |
---|---|---|---|
storageKey | string | ✅ | Specifies where to keep the persisted data in the storage |
permanent | boolean | ❎ | By default the sessionStorage is used to store the persisted data. To change the storage to localStorage you can set the permanent property to true : |