【发布时间】:2019-09-18 06:17:41
【问题描述】:
我正在尝试获得这样的状态形状:
state = {
items: {
currentItem: object,
byId: object,
allIds: array,
fetching: bool
},
someOtherModule = { ... }
}
我已将我的 items reducer 拆分为两个文件,试图不在一个文件中包含所有内容;请注意,这只是伪代码来说明我在做什么 -
items/reducers/currentItem.js
const currentItem = (state = null, action) => ...
export default currentItem;
我试过这样:
items/reducers/items.js
const byId = (state = {}, action) => ...
const allIds = (state = [], action) => ...
const fetching = (state = false, action) => ...
export default {byId, allIds, fetching};
items/reducers/index.js
import { combineReducers } from 'redux';
import item from './item';
import currentItem from './currentItem';
export default combineReducers({ item, currentItem });
我已经尝试过这种方式:
items/reducers/items.js
export const byId = (state = {}, action) => ...
export const allIds = (state = [], action) => ...
export const fetching = (state = false, action) => ...
items/reducers/index.js
import { combineReducers } from 'redux';
import * as item from './item';
import currentItem from './currentItem';
export default combineReducers({ item, currentItem });
我最终得到了一个只有 currentItem 而没有“items”的“root items reducer”。
如果我在 items.js 中导出 combineReducers({byId, allIds, fetching}) 它可以工作,但它会为我的状态增加一个级别:
state = {
items: {
currentItem: object,
items: {
byId: object,
allIds: array,
fetching: bool
}
},
【问题讨论】:
标签: javascript ecmascript-6 redux es6-modules