@converge/state
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

@converge/state · Version License JavaScript Standard Style

Minimal state management library similar to redux, with immer 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

Package Sidebar

Install

npm i @converge/state

Weekly Downloads

2

Version

1.1.0

License

MIT

Unpacked Size

5.95 kB

Total Files

6

Last publish

Collaborators

  • haltcase
  • citycide