# js-stateful

> Vanilla JS State Management

Latest version **0.0.2** (published 2022-03-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install js-stateful
pnpm add js-stateful
yarn add js-stateful
bun add js-stateful
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2022-03-28 |
| First published | 2019-11-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 11 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Samuel Hornsey |
| Maintainers | samhornsey |
| Keywords | State, Management, Vanilla, JS |

## Links

- npm: https://www.npmjs.com/package/js-stateful
- Repository: https://github.com/SamuelHornsey/js-stateful
- Homepage: https://github.com/SamuelHornsey/js-stateful#readme
- Issues: https://github.com/SamuelHornsey/js-stateful/issues
- npm.io page: https://npm.io/package/js-stateful

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.0.2 (latest) — 2022-03-28
- 0.0.1 — 2019-11-25
- 0.0.0 — 2019-11-25

## README

# Javascript State Management

State management for your vanilla JS apps 📫

## Demo

View a demo here: [https://samuelhornsey.github.io/js-stateful/demo/](https://samuelhornsey.github.io/js-stateful/demo/)

## Installation

Installing using npm

```
npm install js-stateful
```

import as module.

```js
import Store from 'js-stateful'
```

## Usage

Setting up and creating the store

```js
import Store from 'js-stateful'

// Initial state
const state = {
  items: []
}

// Create an object containing mutations
const mutations = {
  ADD_ITEM: (state, action) => {
    state.items.push(action.item);

    return state;
  }
};

// Init the store
const store = new Store({ state, mutations, debug: true });
```

You can pass in a onChange handler. Everytime the state is changed this handler will be called.

```js
store.onChange(state => {
  console.log(state);
});
```

To modify the state use an action. These will call a specific mutation. The dispatch function on the store will create and commit the action.

```js
// Create an action function
const addItem = item => {
  return { type: "ADD_ITEM", item };
};

store.dispatch(addItem({ title: 'Hello, World' }));
```

To read the current state of the store simple use it like an other object.

```js
console.log(store.state);
```

---
_Source: https://npm.io/package/js-stateful · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
