@zesertebe/min-store
v1.0.8
Published
Simple Redux-like state manager
Downloads
46
Readme
min-store
A simple state management library inspired by Redux. min-store allows you to manage application state efficiently and subscribe elements to state changes.
Table of Contents
Install
npm install @zesertebe/min-storeUsage
Creating a Store
import { StoreCreator } from "@zesertebe/min-store";
type StoreType = {
user: {
name: string;
email: string;
isLogged: boolean;
};
pages: { [key: string]: { home: { isActive: boolean } } };
};
const MY_STORE: StoreType = {
user: {
name: "",
email: "",
isLogged: false,
},
pages: {
home: {
isActive: true,
},
},
};
const store_ = StoreCreator(MY_STORE, ["user.isLogged"]);
export const STORE = store_;If you need the type definition of your object in the get method:
export const STORE: Omit<typeof store_, "get"> & {
get: StoreType;
} = store_;Updating State
store.set("user.isLogged", true);Getting State
console.log(store.get.user.isLogged); // trueSubscribing to Changes
const userSubscription = store.subscribe({
func: () => {
if (store.get.user.isLogged) {
console.log("Login success!");
} else {
console.log("Login fail");
}
},
property: "user.logged",
});Unsubscribing
store.unsubscribe(userSubscription);Features
- Simple and lightweight (~2KB minified)
- Supports nested state updates
- Immutable
getmethod to prevent direct modifications - Event-driven subscriptions
