Skip to content
200 byte state management.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
README.md
index.js
package-lock.json
package.json

README.md

picostate

250 byte state management.

Install

npm i picostate --save

Usage

Create a store with initial state:

import createStore from 'picostate'

const store = createStore({
  count: 0
})

Retrieve the store's state (cannot be mutated):

store.state // => { count: 0 }

Add listener(s):

const unlisten = store.listen(state => console.log('Updated:', state))
store.once(state => console.log('Only fires once:', state))

Update state:

const emitUpdate = store.hydrate({ count: 1 })
// or
const emitUpdate = store.hydrate(state => ({ count: state.count + 1 }))

Trigger listener(s):

emitUpdate() // => Updated: { count: 1 }

Run callback on the next tick after update:

store.hydrate({
  count: 1
})(() => console.log('Updated!'))

Unlisten:

unlisten() // => undefined

Reset to initial state:

store.reset()

Replace entire state object (use caution):

store.replace({ count: 0 })

Usage With UI Libraries

License

MIT License © Eric Bailey

You can’t perform that action at this time.