Bomber
Bomber

Reputation: 10977

Unexpected key found in preLoadedState expected to find one of the known reducers keys instead

I am trying to set the defaultState to some dummy json to create some select lists:

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

I get an error:

Unexpected key studentYears found in preLoadedState expected to find one of the known reducers keys instead.

How can I add some dummy json to my component?

// 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;

Upvotes: 3

Views: 2740

Answers (1)

Shubham Khatri
Shubham Khatri

Reputation: 282030

According to the redux docs:

If you produced reducer with combineReducers, this must be a plain object with the same shape as the keys passed to it.

So your InitialState should be like

    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"]
              }
          ]
        }
    };

Assuming navigationReducer contains the studentYears object

Upvotes: 2

Related Questions