NModel
Lightweight wrapper for redux and redux-thunk.
Install
npm install --save nmodel
or use yarn:
yarn add nmodel
Checkout examples
Features
- Easy to use
- Support load model dynamically
- Less API
Usage
1. Create redux store
// store.js;const store = ;
2. Define a model
// model.js; const m =
3. Connect to components
// container.js;; const mapDispatchToProps = ...modeleffects;const mapStateToProps = ...statemodelnamespace;mapStateToProps mapDispatchToPropsSomeComponent;
Model
Model is just a part of redux store. A model will be initialized with a object which contains:
- namespace: Must be unique in app, used to ensure the model is unique.
- state: State data.
- effects: The same with redux's actions, but nmodel will inject some methods to update state more easily.
- reducers: The same with redux's action handlers
- onError: Triggered when effect throws error or rejects a promise
methods injected to effect:
- update(state): Update model state.
- put(action): Dispatch an action inside model. The action handler is defined in model's
reducers
, soaction.type
doesn't need to add prefix. - dispatch(action): Dispatch an action.
action.type
have to be prefixed with${model.namespace}/
- getState: Get store's whole state
- getModelState: Get the model state
API
createStore(initialState, middlewares, enhancers)
Create the redux store.
model(modelObject)
Define a model.