Skip to content

t8js/store

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm Lightweight TypeScript ✓ browser ✓ node ✓

@t8/store

Vanilla JS/TS data store

Installation: npm i @t8/store

Initialization

import { Store } from "@t8/store";

let store = new Store({ counter: 0 });

🔹 Similarly to instances of the built-in data container classes, such as Set and Map, stores are created as new Store(data) rather than with a factory function.

Manipulation

let state = store.getState();
console.log(state.counter); // 0

store.setState({ counter: 100 });
console.log(state.counter); // 100

store.setState(state => ({ ...state, counter: state.counter + 1 }));
console.log(state.counter); // 101

Subscription to updates

let unsubscribe = store.onUpdate((nextState, prevState) => {
  console.log(nextState, prevState);
});

unsubscribe();