Skip to content

Latest commit

 

History

History

store

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

@fiad/toolbox/store

A simple state management handler


Get started

npm i @fiad/toolbox
import Store from '@fiad/toolbox/store'

Methods

constructor

const store = new Store({ ...initialState })

get

It retrieves the whole current state or the current value of the given property.

const state = store.get()

// or if only one property is needed
const someProp = store.get('someProp')

set

It updates the values of the given properties, while the rest of the state remains preserved.

store.set({ someProp: 'newValue', otherProp: 'otherValue' })

// or if only one property needs to be updated
store.set('someProp', 'newValue')

reset

It resets the state to its initial value.

store.reset()

observe

It adds a listener that runs the given callback when the given property is updated.

const callback = value => console.log(value)

store.observe('someProp', callback)

// the updated value of someProp will be logged in the console on change

unobserve

It removes the listener registered for the given property.

store.unobserve('someProp', callback)