Reducer gives an invalid attempt spread non-iterable instance

Viewed 388

Would like to know whats wrong with my code it gives an error when i try dispatching a action using redux this is my reducer.js code

import {UPDATE_LOGIN} from './constants'

const initialState = {
    isLogged:false
};

const reducer = (state = initialState, action) => {
    switch (action.type) {
        case UPDATE_LOGIN:
          return [
            ...state,
            {
              isLogged:action.isLogged
            }
          ];
        default:
          return state;
      }
}
export default reducer;

it gives a Invalid attempt to spread non-iterable instance. In order to be iterable, non-array objects must have a Symbol.iterator method.

1 Answers

Your state is an object, and you try to destructure it as an array. In addition, you try to include the object, instead of changing the property. Try this:

const reducer = (state = initialState, action) => {
  switch (action.type) {
    case UPDATE_LOGIN:
      return {
        ...state, 
        isLogged: action.isLogged
      };
    default:
      return state;
  }
}
Related