@stately/core
v0.0.3
Published
pure js state Managment
Downloads
12
Maintainers
Readme
stately
Easy state management for javascript. it's useful for global state management and complex local state
TOC
install
npm i @stately/core --save
yarn add @stately/core
use
import React from "react";
import { createStore } from "@stately/core";
const initialState = {
counter: 0
};
const actions = {
addToCounter: (store, amount) => {
const newCounterValue = store.state.counter + amount;
store.setState({ counter: newCounterValue });
}
};
const store = createStore(initialState, actions);
property
| Method | Return Type | params |
| ----------------------------------------------------------------- | ------------------- | :--: |
| store.setState() | void
| partialState:object
|
| store.addListener() | { remove():void }
| listener: Function
, sensitiveStatesKey: Array<string>
|
| store.actions() | object
| any
|
| store.state | string
| |
setState()
Set partial state
Examples
store.setState({counter: store.state.counter+1})
addListener()
Add an event listener. Listener run when a state did update Examples
below event run just counter did update
function logCounter(){
console.log(store.state.counter)
}
store.addListener(logCounter,["counter"])
below event run when any change in state did invoke
function logState(){
console.log(store.state)
}
store.addListener(logState,[])
actions()
gives initialed actions
Notes
Store is bound in first params.
Examples
store.actions.addToCounter(3)
state
gives current state
Examples
console.log(store.state.counter)