React Redux 意外键传递给创建商店 [英] React Redux unexpected key passed to create store

查看:22
本文介绍了React Redux 意外键传递给创建商店的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误 在传递给 createStore 的 initialState 参数中发现意外的关键字符".期望找到已知的 reducer 键之一:marvelReducer"、routing".意外的键将被忽略.

根减速器:

 import { combineReducers } from 'redux';
 import { routerReducer } from 'react-router-redux';
 import marvelReducer from './marvelReducer';

 const rootReducer = combineReducers({
   marvelReducer,
   routing: routerReducer
 });
 export default rootReducer;

奇迹减速器:

import { FETCH_MARVEL } from '../constants/constants';
import objectAssign from 'object-assign';

export default function marvelReducer(state = [], action) {
  switch (action.type) {
    case FETCH_MARVEL:
      return objectAssign({}, state, {characters: action.data});

    default:
      return state;
  }
}

商店:

import { createStore } from 'redux';
import { syncHistoryWithStore } from 'react-router-redux';
import { browserHistory } from 'react-router';

import rootReducer from '../reducers/index';

const initialState = {
  characters: []
};

const store = createStore(rootReducer, initialState);

export const history = syncHistoryWithStore(browserHistory, store);

if (module.hot) {
  module.hot.accept('../reducers/', () => {
    const nextRootReducer = require('../reducers/index').default;
    store.replaceReducer(nextRootReducer);
  });
}

export default store;

我在另一个应用程序中有非常相似的代码,并且运行良好.不知道这里发生了什么

I have very similar code in another application and it's working fine. Not sure what's going on here

推荐答案

你设置为 store 的初始状态和你告诉 store 期望 store 的初始状态应该是什么之间有一个小的不匹配,例如- 更新商店的初始状态设置:

There's a small mismatch between what you set as the initial state of the store and what you tell the store to expect what the initial state of the store should be, e.g. - update your initial state setting for the store as such:

const initialState = {
   marvel: {
     characters: []
   }
};

而且最好将状态树变量持有者命名为有意义的名称,其中不包含reducer,因此更新

And also it's a good idea to name your state tree variable holders to meaningful names that do not contain reducer in them, so update

const rootReducer = combineReducers({
   marvelReducer,
   routing: routerReducer
});

const rootReducer = combineReducers({
   marvel: marvelReducer,
   routing: routerReducer
});

这应该对你有用.

希望这会有所帮助,

附注.一些文档.

来自文档:

如果您使用 combineReducers 生成了 reducer,则这必须是一个与传递给它的键具有相同形状的普通对象.否则,您可以随意传递您的减速器可以理解的任何内容.

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.

如果你不需要处理与onetwo相关的任何动作,只需在最初将它们拉入,这可能像

If you don't need to handle any actions related to one or two, just pull them in initially, this could be as simple as

export default combineReducers({
  events,
  flash,
  one: (state = {}) => state,
  two: (state = {}) => state
})

这篇关于React Redux 意外键传递给创建商店的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆