Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@abasb75/state-manager

Package Overview
Dependencies
Maintainers
0
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@abasb75/state-manager

a javascript powerfull state manager

Source
npmnpm
Version
0.0.0-test6
Version published
Weekly downloads
43
Maintainers
0
Weekly downloads
 
Created
Source

@abasb75/state-manager

@abasb75/state-manager is a great and best state manager tools for your javascript web applications.

  • sync data between opened browser tabs when updating state.
  • save data on localstorage
  • great type hint for state or defined action

installation

npm i @abasb75/state-manager --save

Quick Start:

  • Create initial value for passing to store's object:

interface StateType {
    darkMode:boolean;
    counter:number;
    notes:{
        text:string;
        date:number;
    }[];
}

const initialState:StateType = {
    darkMode:false,
    counter:0,
    notes:[],
}

  • Define your actions:

const actions = {
    toggleDarkMode:(state:StateType)=>{
        return {
            ...state,
            darkMode:!state.darkMode,
        };
    },
    counter:{
        increment:(state:StateType)=>{
            return {
                ...state,
                counter:state.counter+1,
            }
        },
        decrement:(state:StateType)=>{
            return {
                ...state,
                counter:state.counter-1,
            }
        },
    },
    notes:{
        add:(state:StateType,text:string):StateType=>{
            console.log('add worked!')
            return {
                ...state,
                notes:[
                    ...state.notes,
                    {
                        text:text,
                        date:Date.now(),
                    }
                ]
            }
        },
        delete:(state:StateType,id:number):StateType=>{
            return {
                ...state,
                notes:state.notes.filter(n=>n.date!==id),
            }
        },
    }
}

  • Create your store:

import { createStore } from "@abasb75/state-manager";

...

const initialState:StateType = {
    ...
}


const actions = {
    ...
}

const store = createStore({
    initialState,
    actions,
    storgable:true, // if storagble sets true, states saved on localstorage
    storageKey:'mystorage',
});

export default store;

  • import store everywhere you neeed it:

import store from './store';

  • get state data with get method:

const state = store.get(); //return state

...

const state = store.get(state=>state.counter); //return counter value 

  • update state properties value with set method:

store.set({
  counter:0;
}).then(state=>{
  console.log(state.counter);
});

  • update state value with defiened actions:
store.getActions().counter.increment();

  • For subscribe state changes:
const unsubscribe = (state)=>{
    console.log(state);
}

// subscribe return itself unsubscribe
unsubscribe();

// alternative for get subscriber:
const subscribeId = store.addSubscriber((state)=>{
    console.log(state);
});

// and  unsubscribe with subscribeId:
store.unsubscribe(subscribeId);

Examples:

Simple Note App

Keywords

state

FAQs

Package last updated on 02 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts