Bomber
Bomber

Reputation: 10947

Redux reducer error - assign MUST be an object

I get the following error in my reducer return statement.

In this environment the sources for assign MUST be an object. This error is a performance optimization and not spec compliant.

export function setCredentials(state = {}, action) {
    switch (action.type) {
        case "SET_YEAR":
            return { ...state, ...action.setYear };
        case "SET_STUDENT":
            return { ...state, ...action.setStudent };
        case "SET_GROUP":
            return { ...state, ...action.setGroup };
        default:
            return state;
    }
}

Action creators:

export const setYear = int => ({
    type: "SET_YEAR",
    setYear: int
});

export const setGroup = int => ({
    type: "SET_GROUP",
    setGroup: int
});

export const setStudent = int => ({
    type: "SET_STUDENT",
    setStudent: int
});

I am trying to create the following state:

{
      setCredentials: {
                 setYear: 20,
                 setStudent:10,
                 setGroup: 10
      }
}

Any ideas?

Reducer/index.js

import { combineReducers } from "redux";
import {
    setCredentials,
    fetchCategories,
    eventsForMonth,
    fetchEvents
} from "./events";

import { hasErrored, isLoading } from "./loader";

import navigationReducer from "./navigationReducer";
const rootReducer = combineReducers({
    fetchEvents,
    navigationReducer,
    hasErrored,
    eventsForMonth,
    isLoading,
    fetchCategories,
    setCredentials
});

export default rootReducer;

Store:

import rootReducer from "../reducers";

const initialState = {
    hasErrored: false,
    isLoading: true,
    fetchEvents: {},
    fetchCategories: [],
    setCredentials: {}
};

const reduxLogger = createLogger();

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

export default store;

Upvotes: 0

Views: 507

Answers (1)

Miguel Calderón
Miguel Calderón

Reputation: 3091

In your reducers you're trying to destructure values, not objects. You'll understand it on seeing this proposed change:

export function setCredentials(state = {}, action) {
    switch (action.type) {
        case "SET_YEAR":
            return { ...state, setYear: action.setYear };
        case "SET_STUDENT":
            return { ...state, setStudent: action.setStudent };
        case "SET_GROUP":
            return { ...state, setGroup: action.setGroup };
        default:
            return state;
    }
}

Upvotes: 1

Related Questions