1.0.1 • Published 5 years ago
@muzikanto/observable-persist v1.0.1
Observable-persist
Introduction
- based on observable
- save state to localStorage and get state on initialize page
Installation
npm i @muzikanto/observable @muzikanto/observable-persist
# or
yarn add @muzikanto/observable @muzikanto/observable-persist
Example
save and get from localStorage
const store = persistStore<number>(1, 'localStorage-key', strategyLS);
const inc = createEvent();
store.on(inc, (s) => s + 1);
function Example() {
const state = useStore(store);
return (
<div>
state: {state}
<button onClick={() => inc()}>inc</button>
</div>
);
}
Advanced with transform state
const store = persistStore<number>(
1,
'localStorage-key',
strategyLS,
[
// transform deprecated state to normal
{id: 'to-array', func: (value: number) => [value]},
],
);