0.0.3 • Published 6 years ago

@stately/core v0.0.3

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

stately

Easy state management for javascript. it's useful for global state management and complex local state

NPM Version download

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

MethodReturn Typeparams
store.setState()voidpartialState:object
store.addListener(){ remove():void }listener: Function, sensitiveStatesKey: Array<string>
store.actions()objectany
store.statestring

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)