1.0.0 • Published 3 years ago

rstates v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

RStates

A super small, simple and fast ⚡ JavaScript state library

NPM

GitHub issues license npm bundle size npm

Why a fork?

RStates uses an implementation similar to React's useState hook which alleviates issues such as removing empty properties and being unable to use arrays directly within the state.

Install

npm i rstates

Usage

const rstates = require('rstates')

const myState = rstates({ counter: 0 })

function onUpdate(state) {
	console.log('onUpdate: counter changed to ', state.counter)
}

myState.subscribe(onUpdate)

// Updating with an object
myState.setState({ counter: ++myState.state.counter })
// => onUpdate: counter changed to  1

// Updating with a function
myState.setState(state => ({ counter: ++state.counter }))
// => onUpdate: counter changed to  2

API

const initialState = {};
const stateInstance = rstates(initialState);
/* => returns state Instance
{
  state,
  subscribers,
  setState,
  subscribe,
  unsubscribe,
};
*/

// Get the state
stateInstance.state;

// Change the state
stateInstance.setState(<object or a function that returns and object>);
// => returns a promise

// Subscribe to state changes
stateInstance.subscribe(<function that will be called with the state on each update>);


// Unsubscribe from state changes
stateInstance.unsubscribe(<function that already subscribed>);