1)項目結(jié)構(gòu)
常見單頁面的redux
項目,結(jié)構(gòu)為:
|.
├── config // 打包的配置文件
├── dist
├── node_modules
├── package.json
├── readme.md
├── server
└── src // 開發(fā)目錄
其中src
目錄為:
├── components // 與redux無關(guān)的組件
├── constant // 常量靶累,一般大寫聲明
├── containers // 與redux相關(guān)的組件腺毫,一般為項目模塊
├── index.html // 入口模版文件
├── index.js //
├── middleware // 自定義的中間件
├── redux // 存放store,action挣柬,reducer潮酒;分模塊劃分redux
├── route // 路由
└── static // 公用的靜態(tài)js,css邪蛔,images
其中redux
目錄為:
.
├── createStore.js // import各種(異步)中間件急黎,Reducers,創(chuàng)建store
└── module // 按模塊劃分的action和reducer
├── Contact
│ ├── curr.js // action和reducer寫在一個文件中
├── Desk
│ └── getIndexSetting.js
└── index.js // import模塊中的reducer侧到,export combineReducers
2)中間件
A thunk is a function that wraps an expression to delay its evaluation.
a.用到的中間件:
- redux-thunk 支持異步action
// action 的命名根據(jù)模塊來區(qū)分
// 例如:const USERINFO = 'contact/USERINFO'
export const test = (params)=> {
return (dispatch, getState)=> {
// do something
dispatch(otherAction)
}
}
const store = createStore(reducers, {}, applyMiddleware(
thunkMiddleware,
errorMiddleware,
promiseMiddleware(),
loggerMiddleware
));