You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
<scripttype="module">importLitestatefrom'//unpkg.com/litestate';conststore=Litestate({state:{firstName:'',lastName:'',count:0,/** Computed state: function that returns a new value to the state **/fullName(state){return`${state.firstName}${state.lastName}`},},/** Action Mutators: functions to update the state **/setFirstName(state,firstName){state.firstName=firstName;},setLastName(state,lastName){state.lastName=lastName;},// Async exampleasyncmakeAjaxCall(state,url){state.pending=true;// The state will be mutatedstate.data=awaitfetch(url);state.pending=false;// The state will be mutated},// Other action mutatorsinc(state)=>state.count++,dec(state)=>state.count--,});</script>
State
State are the values that can be initially set, or set via an action mutator.
Initial state can be set during initialization of the store, in thestate property.
Thestate property is an object that may contains: String, Number, Array, Plain Object, Boolean, Null, Undefined.
If the value of a state property is a function it will be converted into a computed state (read more below)
You can access the full state with the methodLitestate.$state() or by using any state properties.
// get full state objectconstmyFullState=store.$state();// or individual propertyconstname=store.name;constversion=store.version;
Actions Mutators
Action mutators are functions that can mutate the state in place. They accept the currentstate as the first argument and can return any values.
Action mutators are set during initialization of the store.
[v.0.12] Action mutators can also call other actions by usingthis.
conststore=Litestate({setFirstName(state,firstName){state.firstName=firstName;},setLastName(state,lastName){state.lastName=lastName;},// Async exampleasyncmakeAjaxCall(state,url){state.pending=true;// The state will be mutatedstate.data=awaitfetch(url);state.pending=false;// The state will be mutated},// Other action mutatorsinc(state)=>state.count++,dec(state)=>state.count--,// This action will call other actionsasynccallMe(state){this.inc();awaitthis.makeAjaxCall();}})
Usage
Run an action mutator by using the function name
store.setFirstName('Mardix');store.setLastName('M.');store.inc();// will increment the countstore.dec();// will decrement the countstore.callMe();// will call other actions
Computed State
Computed State are function that select part of the state to create new properties.They are function defined along with the initial state.The selected value will be assigned to the function's name
conststore=Litestate({state:{firstName:'',lastName:'',count:0,/** Computed state: function that returns a new value to the state **/fullName(state){return`${state.firstName}${state.lastName}`},}, ...});
A new propertyfullName will be assigned to the state and will contain the returned value.
Usage
The same way you would access the initial state, computed states are accessed the same way, by calling the property.
constfullName=store.fullName;
Subscribe to changes
You can subscribe to changes in the state. Each time the state is updated it will run a function that you set.