【发布时间】:2018-04-24 20:47:33
【问题描述】:
我似乎无法从 reducer 更新 redux 存储。状态是一个immutable.js 记录,在图像中打印得很漂亮。
这个减速器运行:
const searchResultsMapRegion = (state = vepo, action) => {
switch (action.type) {
case 'UPDATE_SEARCH_PAGE_RESULTS_PAGE_MAP_REGION': {
console.log(state.toJS())
const newState = state.set('some', 555)
console.log(newState.toJS())
return newState
}
default:
return state
}
}
const rootReducer = combineReducers({
product: product,
searchResultsMapRegion: searchResultsMapRegion
})
export const rootEpic = combineEpics(
fetchCategoriesEpic,
getUserPositionEpic,
getUserLocationEpic,
getUserLocationFulfilledEpic
)
export const store = createStore(
rootReducer,
vepo,
composeWithDevTools(applyMiddleware(createEpicMiddleware(rootEpic)))
)
我有其他减速器作用于状态的product 部分,它们工作正常。为什么我不能对问题中显示的状态的some 部分进行操作?
console.log of state 在 reducer 顶部:
console.log of newState 在 reducer 内
reducer 完成运行后state 的console.log:
【问题讨论】:
-
state的值是多少?state.set做了什么并返回? -
@FelixKling
state是一个immutable js record。我有控制台使用toJS()函数记录它以漂亮地打印它。.set()返回具有修改属性的记录的新副本。 -
您使用的是内置的
combineReducers还是redux-immutable的那个?有关详细信息,请参阅the Redux docs。 -
@MichaelPeyper 我现在已经解决了这个问题。我的 redux 存储的初始状态具有与输入到
combineReducers的属性不同的属性名称。甚至不需要向createStore()提供初始状态吗?我一直在用错误的值设置初始状态。 -
@MichaelPeyper 我现在看到设置初始状态是可选的。
标签: ecmascript-6 redux react-redux immutable.js