effector-persist
v1.0.4
Published
Higher-order function for persist effector store in local storage
Downloads
454
Maintainers
Readme
Effector persist
Micro package for create persists effector store. Like redux-persist.
Example:
import { withPersist } from "effector-persist";
import { createStore, createEvent } from "effector";
const store = withPersist(createStore([], {name: 'shops'}));
const addStore = createEvent<{name: string}>();
store.on(addStore, (state, store) => [...state, store]);
Example with Ramda
import { withPersist } from "effector-persist";
import { createStore } from "effector";
import * as R from "ramda";
const createPersistedStore = R.pipe(createStore, withPersist);
const store = createPersistedStore([], {name: 'shops'});
Example with expired
import { withPersist } from "effector-persist";
import { createStore, createEvent } from "effector";
const store = withPersist(createStore([], {name: 'shops'}), {expire: Date.now() + 2000});
const addStore = createEvent<{name: string}>();
store.on(addStore, (state, store) => [...state, store]);
How it works:
Every store update, withPersist
cache result of update in LocalStorage with key persist:<store_name>
.
When store is created withPersist
restore previous state from snapshot in LocalStorage.