@menthays/easystate
v0.2.1
Published
Easy state management based on React Context and Hooks
Downloads
4
Readme
EasyState
Easy state management based on React Context and Hooks
Install
npm install @menthays/easystate
Example
// store.js
import EasyState from '@menthays/easystate'
const CounterStore = EasyState({
state: {
count: 0
},
actions: {
increment(state, {number}) {
return {
count: state.count + number
}
}
}
});
export default CounterStore
// component
import CounterStore from 'path/to/store.js'
const {Provider, getState, getDispatcher} = CounterStore;
function CounterComponent() {
const state = getState();
const dispatch = getDispatcher();
return (
<Provider>
<div onClick={() => dispatch('increment', {number: 3})}>Increment</div>
<div>{state.count}</div>
</Provider>
)
}