Lightweight wrapper for redux and redux-thunk.
npm install --save nmodel
or use yarn:
yarn add nmodel
- Easy to use
- Support load model dynamically
- Less API
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 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
action.typedoesn't need to add prefix.
- dispatch(action): Dispatch an action.
action.typehave to be prefixed with
- getState: Get store's whole state
- getModelState: Get the model state
createStore(initialState, middlewares, enhancers)
Create the redux store.
Define a model.