Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

142
Vistas
Reducer not deleting items from the state

I am unable to delete items from the redux state. I have created a pizza app and I am trying to delete items from the cart. I have successfully implemented increased quantity or deleting all items in cart functions. However, I am unable to implement a delete a particular item function. My button on the front end are working properly and passing ID to deletePizza function given below

Here is my reducer

export const cartReducer = (state = [], action) => {

    switch (action.type) {
        //Required by next-js-reducer library
        case HYDRATE:
            return { ...state, ...action.id };

        //Removes all pizza from the cart
        case 'DELETE_ALL':
            return []

        //Add pizza to cart, if exists increment the pizza
        case 'ADD_PIZZA':
            //If pizza is already in the cart
            const findPizzaIfAdded = state.find(pizza => pizza.id === action.id);
            if (findPizzaIfAdded !== undefined) {
                const modifiedQuantity = {
                    ...findPizzaIfAdded,
                    quantity: findPizzaIfAdded.quantity + 1
                }
                return state.map(pizza => pizza.id === action.id ? modifiedQuantity : pizza);
            }

            const newPizza = {
                id: action.id,
                quantity: 1
            }

            return [
                ...state,
                newPizza
            ]

        //I am facing problem here
        case 'DELETE_PIZZA':
            return state.filter(pizza => pizza.id !== action.id);

        default:
            return state
    }
}

// I am using this funcntion to call deletion of pizza, I tried testing if particular function is working. It is successfully working and passing the id
export const deletePizza = id => {
    return {
        type: 'DELETE_PIZZA',
        id
    }
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda