在preLoadedState中发现的意外密钥可能会找到一个已知的reducers密钥

时间:2018-01-04 09:40:41

标签: reactjs redux

我正在尝试将defaultState设置为某个虚拟json以创建一些选择列表:

const initialState = {
    eventsHasErrored: false,
    eventsIsLoading: true,
    events: [],
    calendarView: 0,
    studentYears: [
        {
            id: 1,
            title: "Year 1",
            options: ["Firm 1", "Firm 2", "Firm 3"]
        },
        {
            id: 2,
            title: "Year 2",
            options: ["Firm 2", "Firm 3", "Firm 4"]
        },
        {
            Id: 4,
            title: "Year 3",
            options: ["Firm 5", "Firm 6", "Firm 7"]
        }
    ]
};

const reduxLogger = createLogger();

const store = createStore(
    rootReducer,
    initialState,
    applyMiddleware(thunk, reduxLogger)
);

我收到错误:

在preLoadedState中找到意外的关键studentYears,希望找到一个已知的reducers键。

如何在组件中添加一些虚拟json?

// combineReducer
import { combineReducers } from "redux";
import {
    events,
    eventsHasErrored,
    eventsIsLoading,
    calendarView
} from "./events";
import navigationReducer from "./navigationReducer";

const rootReducer = combineReducers({
    events,
    eventsHasErrored,
    eventsIsLoading,
    calendarView,
    navigationReducer
});

export default rootReducer;

1 个答案:

答案 0 :(得分:1)

根据 redux docs

  

如果您使用combineReducers生成reducer,那么这必须是简单的   与传递给它的键形状相同的对象。

所以你的InitialState应该像

    const initialState = {
        eventsHasErrored: false,
        eventsIsLoading: true,
        events: [],
        calendarView: 0,
        navigationReducer: {
          studentYears: [
              {
                  id: 1,
                  title: "Year 1",
                  options: ["Firm 1", "Firm 2", "Firm 3"]
              },
              {
                  id: 2,
                  title: "Year 2",
                  options: ["Firm 2", "Firm 3", "Firm 4"]
              },
              {
                  Id: 4,
                  title: "Year 3",
                  options: ["Firm 5", "Firm 6", "Firm 7"]
              }
          ]
        }
    };

假设navigationReducer包含studentYears对象