relite
a redux-like library for managing state with simpler api (1kb)
Why
redux is awesome, but is not enough simple for small and middle application.
With relite
, we don't need to combine | apply | bind
anything, just write pure function and call actions, it done.
What's new(3.0.0)
-
Supoort
Typescript
. -
Delete the support of return type
Promise | Function
ofAction
.
Installtion
npm install --save relite
How to use
Write pure function
the action of relite
looks like a reducer of redux, but more simple and powerful.
/*** an action consist of action-type, action-payload, action-handler and action-result* at this example* action-type is EXEC_BY* action-handler is the function accepts two arguments: state and action-payload* action-result is the result of function*/ // javascriptlet { let value = return ? state : ...state count: statecount + value } // typescript type State = // ... count: number let EXEC_BY: Action<State number> = { return ? state : ...state count: statecount + input }
Create store by actions and initialState
let initialState = count: 0let store = /** relite will bind state for every actions you gave to `createStore`* so all the functions in store.actions can only accept one argument, action-payload* no need to bindActionCreators* each actions return currentState or promise with currentState*/let INCREMENT EXEC_BY = storeactions // -> { count: 1 } // -> Promise[[{ count: 10 }]] /*** subscribe store by store.subscribe* when the state was changed/updateed, relite would trigger the listeners* if action-handler return the same state, listeners would not be triggered*/let unsubscribe = store let newState = count: 0let simulateData = actionType: 'REPLACE_STATE' actionPayload: null start: end: previousState: store // get current state currentState: newStatelet keepSilent = false // if true, it will not trigger listeners // replace state by store.replaceStatestore // trigger listener by store.publishstore store // dispatch the action manually
End
Issue and pull request is welcome!