deep-state-store
v0.1.2
Published
Simple global state library for javascript.<br><br>
Downloads
2
Maintainers
Readme
deep-state-store
Simple global state library for javascript.
Deep state store is an state management library which will trigger an update only when the state is completely different.
Example
import Store from 'deep-state-store';
const store = new Store({ test: 0, data: ['test']});
store.on(['test', 'data'], (state) => {
console.log(state);
});
store.set({ test: 1 });
store.set({ data: ['test1'] });
store.set({ test: 2, data: ['test1', 'test2', 'test3'] });
store.set({ test: 3, data: ['test2'] });
store.set({ test: 5 });
API
new Store(initialState)
Create instance.
const store = new Store({ test: 0 });
get state
Get current state.
const store = new Store({ test: 0 });
console.log(store.state);
set(nextState)
Update state.
const store = new Store({ test: 0 });
store.set({ test: 1 });
on([deps], fn)
Listening to event when state changes.
const store = new Store({ test: 0, data: 'test' });
store.on(['test', 'data'], (state) => {
console.log(state); // { test: 1, data: 'test2' }
});
store.set({ test: 1, data: 'test2' });
off
Remove Listener
const off = store.on(deps, listener);
off();
License
MIT