本文參考了jackblog-react-native-redux的代碼
/constants/ActionTypes.js
保存action type字符串常量阳藻。不建議寫在對應(yīng)的action creator函數(shù)里面,因?yàn)閞educer也需要type
/actions
保存action creator 函數(shù)。只用于傳遞數(shù)據(jù)赚窃,包括:type矮台,數(shù)據(jù)锰悼,函數(shù)(一般為異步函數(shù),異步操作需要依靠“中間件”(在store中進(jìn)行處理)熊泵。)
例如:
// 目錄: /actions/article.js
/*獲取文章列表*/
function receiveArticleList(json,isMore) {
return {
type: ARTICLE_LIST,
articleList: json.data,
isMore:isMore
}
}
返回的內(nèi)容將當(dāng)作reducers的action參數(shù)值(每個(gè)reducer都可以獲取仰迁,所以type的作用體現(xiàn)出來了)。
/reducers
保存各種數(shù)據(jù)(注意:也許就是state)顽分。包括:網(wǎng)絡(luò)請求結(jié)果轩勘,type。
使用:在對應(yīng)需要數(shù)據(jù)的位置怯邪,例如在某個(gè)組建里面的某個(gè)view绊寻,
獲取reducer
const { reducerA, reducerB } = this.props;
獲取數(shù)據(jù)
reducerA.isRefreshing
reducerA.list
/store/configureStore.js
創(chuàng)建/配置store
import { createStore, applyMiddleware } from 'redux';
import thunkMiddleware from 'redux-thunk';
import rootReducer from '../reducers';
const middlewares = [
thunkMiddleware
];
const createStoreWithMiddleware = applyMiddleware(...middlewares)(createStore);
export default function configureStore(initialStore) {
const store = createStoreWithMiddleware(rootReducer, initialStore);
return store;
}
app.js
root.js