@zenstack/zen-store
TypeScript icon, indicating that this package has built-in type declarations

0.1.3 • Public • Published

Zen Store ☮️

A simple reactive state store

installation

yarn add @zenstack/zen-store

or

npm install @zenstack/zen-store

Usage

Create A State Store

import { createStateStore } from "@zenstack/zen-store";
const initialState = 0;
const stateStore = createStateStore(initialState);

//you can also use a class if you are object oriented
import { StateStore } from "@zenstack/zen-bus";
const initialState = 0;
const stateStore = new StateStore(initialState);

Retrieve State

The store offers an API, .getState() to retrieve the current state

const currentState = store.getState();

Update State

The store offers an API, .setState(:newState) to update the state

const newState = 5;
store.setState(newState);

Subscribe To State

The store offers an API, .subscribe(:handler) to subscribe to updates in the store. The .subscribe(:handler) returns an unsubscribe function that can be executed to detach the handler from the event.

const logState = (state) => console.log(state);

// .subscribe() returns an unsubscribe method
const unsubscribe = store.subscribe(logState);

// logState will be triggered
store.setState('some new state');

// logState will not be triggered after unsubscribing
unsubscribe();
store.setState('some other new state');

/@zenstack/zen-store/

    Package Sidebar

    Install

    npm i @zenstack/zen-store

    Weekly Downloads

    3

    Version

    0.1.3

    License

    MIT

    Unpacked Size

    14.6 kB

    Total Files

    19

    Last publish

    Collaborators

    • alaboudi