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

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

问题描述

我收到错误在传递给createStore的initialState参数中找到了意外的键字符。预计会找到一个已知的reducer密钥:marvelReducer,routing。意外的密钥将被忽略。

rootReducer:

rootReducer :

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

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

marvelReducer:

marvelReducer :

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

推荐答案

你设置为商店的初始状态和你告诉的内容之间存在小的不匹配商店应该期待商店的初始状态应该是什么,例如 - 更新商店的初始状态设置:

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
});

这应该适合你。

希望这有帮助,

PS。一些文档。

来自文档


如果使用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.

如果您不需要处理与一个两个,最初只是拉进去,这可能就像

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天全站免登陆