【问题标题】:Cannot update redux store from reducer无法从 reducer 更新 redux 存储
【发布时间】: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


【解决方案1】:

重新检查案例类型。那是指正确的案例价值吗?另外,vepo 需要从 createStore 中移除。

export const store = createStore(
  rootReducer,
  vepo,
  composeWithDevTools(applyMiddleware(createEpicMiddleware(rootEpic)))
)

【讨论】:

  • 案例类型正确。也许我误解了你,但vepo 是整个应用程序的状态,需要进入createStore,而你在答案中把它留在了里面。
  • rootReducer 将拥有所有数据,只需将其传递给 createstore。
猜你喜欢
  • 2018-10-02
  • 2017-09-28
  • 2021-05-06
  • 2020-08-21
  • 2019-07-28
  • 2020-08-09
  • 2018-09-27
  • 2018-08-04
  • 1970-01-01
相关资源
最近更新 更多