anystate is a tiny state management library for any framework!
- Just an object has observable function
- Can must be update via setter
- Watch state changed
- Independent any frontend frameworks
Back to basic, the callback function is a point. I put handle onChange in callback functions, this is have identify string to make sure target state has been updated. So, that we can get any changes from anywhere.
- Initialize anyState
- Simple state
- getItem/setItem
- Watch onChange
...
Initialize anyState object with createStore()
const anyState = createStore({
name: 'John',
age: 30,
children: [{
name: 'Bob',
age: 5,
}]
});
Set state
anyState.setState({
name: 'John',
age: 32,
children: [{
name: 'Bob',
age: 4,
}]
});
Get state
const state = anyState.getState();
Set item
// const path = 'name';
const path = 'children[0].name'; // the path to the item
anyState.setItem(path, 'Jane');
Get item
const path = 'children[0]';
const child = anyState.getItem(path);
Watch onChange
const path = 'name'; // path to item
anyState.watch(path, (nextState, prevState) => {
// do anything
});
install dependencies:
npm install
run tests:
npm test
dev:
npm run dev
MIT