2.0.1 • Published 5 years ago

dollar-state v2.0.1

Weekly downloads
1
License
MPL-2.0
Repository
github
Last release
5 years ago

$tate

A simple JS state management library - inspired by Redux and based on Immer

license NPM version

Github Issues Build Status Coverage Status

NPM downloads jsDelivr

Installation

Install $tate by running

npm install dollar-state

Usage

Setup

Create a store by running createStore

import $tate from 'dollar-state';

const store = $tate.createStore();

createStore can be invoked with 2 arguments

  • initialState, an object to initialize the state with
  • options, an optional object with the following keys
    • persistenceKey, a string to be used to persist the store to LocalStorage on each update (if required)
    • debug, a flag to enable debug logging on updateState calls, default: false

Update

To update the state you should call the updateState function passing a state modifier function

store.updateState(function(currentState) {
  currentState.count = 1;
});

Subscribe

To subscribe to state changes you simple set a state listener

const _unsubscribeStore = store.subscribe(function(newState) {
  console.log({ newState });
});

N.B: Don't forget to call the returned method when you don't need updates anymore

if (_unsubscribeStore) {
  _unsubscribeStore();
}

Reset state

To reset a store to its initial state

store.resetState();

Demo

A demo using jQuery is available inside the demo folder. To run the demo:

  • build the library using
npm run dev
  • copy (or symlink) dollar-state.min.js file from dist to demo folder
cp dist/dollar-state.min.js demo
  • install demo npm dependencies
cd demo
npm install
  • run demo app
npm run start

Made with :sparkles: & :heart: by Mattia Panzeri and contributors

If you found this project to be helpful, please consider buying me a coffee.

buy me a coffee

2.0.1

5 years ago

1.4.1

5 years ago

2.0.0

5 years ago

1.4.0

6 years ago

1.3.0

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago