Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda