Minimal state management library similar to
redux
, withimmer
built in.
installation
# using yarn:
yarn add @converge/state
# or npm:
npm i @converge/state
usage
import { createStore } from "@converge/state"
const store = createStore({ count: 0 }, {
plus: (by = 1) => state => { state.count += by },
minus: (by = 1) => state => { state.count -= by },
reset: () => () => ({ count: 0 })
})
const { plus, minus, reset } = store.getActions()
const unsubscribe = store.subscribe(() => {
console.log(store.getState())
})
plus()
// -> { count: 1 }
plus(2)
// -> { count: 3 }
minus(2)
// -> { count: 1 }
reset()
// -> { count: 0 }
unsubscribe()
const { state, actions } = store.useStore()
console.log(state)
// -> { count: 0 }
actions.plus(10)
console.log(state)
// -> { count: 0 }
console.log(store.getState())
// -> { count: 10 }
see also
-
redux
– predictable state container for JS apps -
immer
– create the next immutable state by mutating the current one
contributing
Search the issues if you come across any trouble, open a new one if it hasn't been posted, or, if you're able, open a pull request. Contributions of any kind are welcome in this project.
license
MIT © Bo Lingen / citycide