An open API service indexing awesome lists of open source software.

https://github.com/treeder/state

Lightweight JavaScript client side state package using modules.
https://github.com/treeder/state

javascript javascript-library state-management

Last synced: 5 months ago
JSON representation

Lightweight JavaScript client side state package using modules.

Awesome Lists containing this project

README

          

# state

Super lightweight JavaScript state library using ESM modules. Modern and easy to use.

Uses the standard [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) interface.

Features:

* Uses localStorage so state remains intact even if user leaves and comes back.
* Can listen for state changes from anywhere, even across browser tabs!

## Usage

Import this library:

```js

import state from 'https://cdn.jsdelivr.net/gh/treeder/state@1/state.js'

```

Add listeners:

```js
state.addEventListener('car', (e) => {
console.log("car change event:", e.detail)
this.car = e.detail.value
})
```

Update state elsewhere:

```js
state.set('car', car)
```

Fetch state on page load:

```js
state.get('car')
```

Delete from the state:

```js
state.delete('car')
```

💥 That's it!

You can use this anywhere, including in web components.