Anoop K George
Anoop K George

Reputation: 1735

React JS, error in Redux state updation, cannot read property Symbol(Symbol.iterator)

When I update the redux state it gives error TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))

const initialState = {
  articles: []
};

function rootReducer(state = initialState, action) {
 switch(action.type){
   case ADD_ARTICLE:{return [...state,action.payload];}  # error occurs in this line
   default:return state;
 }
}

export default rootReducer;

Upvotes: 2

Views: 140

Answers (1)

norbitrial
norbitrial

Reputation: 15166

You are trying to clone the state which is {} and not []. Try to return as the following:

switch(action.type) {
   case ADD_ARTICLE: {
     return {
        ...state,
        action.payload
     };
   }

   default: {
     return ...state;
   }
}

Upvotes: 3

Related Questions