本文参考了jackblog-react-native-redux的代码
/constants/ActionTypes.js
保存action type字符串常量。不建议写在对应的action creator函数里面,因为reducer也需要type
/actions
保存action creator 函数。只用于传递数据,包括:type,数据,函数(一般为异步函数,异步操作需要依靠“中间件”(在store中进行处理)。)
例如:
// 目录: /actions/article.js
/*获取文章列表*/
function receiveArticleList(json,isMore) {
return {
type: ARTICLE_LIST,
articleList: json.data,
isMore:isMore
}
}
返回的内容将当作reducers的action参数值(每个reducer都可以获取,所以type的作用体现出来了)。
/reducers
保存各种数据(注意:也许就是state)。包括:网络请求结果,type。
使用:在对应需要数据的位置,例如在某个组建里面的某个view,
获取reducer
const { reducerA, reducerB } = this.props;
获取数据
reducerA.isRefreshing
reducerA.list
/store/configureStore.js
创建/配置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