m2-redux
The package is provided factory and utilities based on redux.
You can learning the M2 architecture via visiting: https://app.yinxiang.com/fx/c541e662-a573-47e0-af9c-b98d4ca52714
Usage
- Install
npm install m2-reduxyarn add m2-redux
APIs
Root
class The application Root component with react and redux, will connect the store and router.
/* 有以下几种使用方式: */// 1. 通过Redux Root组件渲染 // 2. 通过Redux Root组件渲染多组件(使用components属性)
- connect function Encapsulate connect from 'react-redux' and provide "reducers", "actions" and "types" options. #### | param | type | description | default | example | | ------------ | ------------ | ------------ | ------------ | ------------ | | reducers | string/array | redux feature name(s) | | 'home', ['home', 'auth'], ['home,auth'] | | actions | object | redux action(s) from the feature | | { getDataList, getDictList } | types | string/array | dictionary name(s) | | 'gender', ['userType', 'gender'], ['userType,gender'] |
@Component // ... { const getDataList getDictList = thisprops } { const home auth = thisprops return <div> homelist -- authuser </div> }
- ReduxFactory class The factory will create store, initialState, actionType, action(sync/async), reducer(sync/async/feature/app).
prop or func | type | description |
---|---|---|
createStore | func | create the redux store with the params rootReducer, {configThunk, routeType, defaultRoute, middlewares}(eg: thunk,saga,logger) |
createInitialState | func | create the initialState based on feature reducer with the params config |
createActionType | func | create the action type with the params config (for emit async event) |
createAction | func | create the sync action with the params params{config,actionKey[,actionType]},payload |
createAsyncAction | func | create the async action with the params promise,params{config,actionKey[,actionType]},handler |
createReducer | func | create the sync reducer with the params state,action,params{config,actionKey[,actionType]},handler |
createAsyncReducer | func | create the async reducer with the params state,action,params{config,actionKey[,actionType,resultField]} |
createFeatureReducer | func | create the feature reducer with the params reducers,state,reducer,handler |
createAppReducer | func | create the app reducer with the params reducers,state,reducer |
clearRedux | func | clear all redux data(only when user will exit the app) |
// create the store const store = ReduxFactoryconst checkIsAuth = storeauthloginUserauthenticated store checkIsAuth // create the action & reducer (sync) const params = config actionKey: 'list' const action = ReduxFactoryconst reducer = ReduxFactory // create the action & reducer (async) const params = config actionKey: 'dict' const promise = commonServicegetDictList const action = ReduxFactoryconst reducer = ReduxFactoryconst type = ReduxFactory // create the feature reducer const reducers = getDataListReducer getDataItemReducer const initialState = ReduxFactory ReduxFactory // create the app reducer const reducerMap = common: commonReducer home: homeReducer auth: authReducer ReduxFactory
- Root class The root component integrate Router, Store by Provider.
prop or func | type | description |
---|---|---|
store | object | the app redux store created by ReduxFactory |
routes | array | the app router included all component routes |
routeType | string | the route type default as 'hash' |
checkIsAuth | func | check if the user is authenticated |
redirectUrl | string | if the user isn't authenticated, it will redirect the page (usually login) |
redirect404 | string | if the route doesn't be matched, it will redirect the page (usually 404) |