Home > Enterprise >  Unable to iterate over an array in state in reducer
Unable to iterate over an array in state in reducer

Time:09-26

I am trying to update my cart using redux but I constantly get this error:

TypeError: state.products is not iterable

my reducer looks like:

/* eslint-disable import/no-anonymous-default-export */
const initialState ={
    products:[]
}
export default (state=initialState,action)=>{
    switch(action.type){
        case "ADD_TO_BASKET":
            return {...state,products:[...state.products,action.payload]};

        default:
            return "";
    }
}

CodePudding user response:

The issue is

        default:
            return "";

You probably want to return state as it is instead.

        default:
            return state;
  • Related