reactjs – React-Redux:组合reducers:意外的键

前端之家收集整理的这篇文章主要介绍了reactjs – React-Redux:组合reducers:意外的键前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的应用程序在我开始结合我的Redux reducer之前工作正常。但是当我结合起来,initialState和reducer键会混合起来。
function flash(state = [],action) {
  switch (action.type) {
  case FLASH_MESSAGE_UPDATED:
    return _.extend({},state,{ flash: action.flash })
  default:
    return state
  }
}

function events(state = [],action) {
  switch (action.type) {
  case EVENTS_UPDATED:
    return _.extend({},{ events: action.pathway_events })
  default:
    return state
  }
}

export default combineReducers({
  events,flash
})

这导致功能不正确和控制台错误

在initialState参数中发现意外的键“一”,“二”,传递给createStore。期望找到一个已知的reducer键,而不是“事件”,“闪存”。意外的键将被忽略。

我的初始状态在redux-thunk的帮助下传入。

import { createStore,applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import reducer from '../../reducers/event'

let initialState = {
  one: globalData.one,two: globalData.two,events: globalData.events,flash: globalData.flash
}
let createStoreWithMiddleware = applyMiddleware(thunk)(createStore)
let reduxStore = createStoreWithMiddleware(reducer,initialState);

React.render(
  <Provider store={reduxStore}>
    <EventListContainer />
  </Provider>,$('.events')[0]
)

如何正确地组合reducer?

我想你只需要添加reducers的附加键,例如
function one(state = {},action) {
  switch (action.type) {
  case ONE_UPDATED:
    return action.one
  default:
    return state
  }
}

the docs

If you produced reducer with combineReducers,this must be a plain object with the same shape as the keys passed to it. Otherwise,you are free to pass anything that your reducer can understand.

如果您不需要处理与一个或两个相关的任何操作,只需将它们初次拉出,这可能就如此简单

export default combineReducers({
  events,flash,one: (state = {}) => state,two: (state = {}) => state
})
原文链接:https://www.f2er.com/react/301401.html

猜你在找的React相关文章