zstate
v1.1.0
Published
Minimalist state manager, thought to sustain a state based on a mutable object that scales according to nodes.
Downloads
7
Readme
zstate
Minimalist state manager, thought to sustain a state based on a mutable object that scales according to nodes.
Nodes?
The nodes are instances that are associated with the state, these instances allow isolated work, example:
import createState from "zstate";
const stateA = createState();
const stateB = A("b");
stateA.on(() => {
console.log("update");
});
stateB.set({ name: "Uppercod" });
Del ejemplo podemos destacar :
stateA
contiene astateB
.stateB
nunca conoce astateA
.
Install
npm install zstore
Usage
import state from "zstate";
const state = store({
toggle: false,
});
state.on(() => {
console.log("update");
});
const toggle = ({ toggle }) => ({ toggle: !toggle });
state.set(toggle); // true
state.set(toggle); // false
Instance
import createState from "zstate";
const app = createState(initialState);
Creates an instance of the state that returns a function that contains the following static properties and methods:
state
app.state;
Current state.
set
app.set({ any: "value" });
Update current state, set
does not replace current state, just merge.
If the property to update is defined as null or undefined, example {any: null}
, it will be removed from the object. if this property is a node, its effects on the parent will be eliminated.
on
const off = app.on((state) => {
console.log("update:", state);
});
It subscribes to state changes.