@trinitymirrordigital/state-manager
v1.5.0
Published
Managing state in web components
Downloads
43
Maintainers
Keywords
Readme
@trinitymirrordigital/state-manager
For managing state in ET web components
Usage
import { State } from '@trinitymirrordigital/state-manager';
// Basic set up - pass keys you want to track.
const state = StateManager(['foo']); // will track changes to foo
// or you can pre-assign values
const state = StateManager(['foo'], {foo: 'bar'}); // will track changes to foo
// Add observer which will be called when there is a change to foo
state.addObserver = (state, oldState)=>{
// do something with change
}
// basic usage
state.foo = 'something'; // add observer called
// observer called with state {foo: 'something'} and oldState {foo: 'bar'}
state.foo = 'something'; // observer not called
// mass assign
state.massAssign = { foo: 'something else'}
// observer called with state {foo: 'something else'} and oldState {foo: 'something'}
// Get state
state.getState // return { foo: 'something else'}