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

463
Vistas
How can I delete cart item on index using Redux?

I want to delete a product I click on inside my cart using Redux. I am able to delete a product with "pop" but it obviously deletes the last item on the list instead of the one I click on. Each product has it's own price that I want to subtract from the total as well. The product and it's price have the same index in the products list and shoePrice list.

import {createSlice} from "@reduxjs/toolkit";

const cartSlice = createSlice({
    name:"cart",
    initialState:{
        products: [], 
        quantity: 0,
        total: 0,
        shoePrice: [], 
},

reducers:{
    addProduct:(state, action)=>{
        state.quantity += 1;
        state.products.push(action.payload);
        state.shoePrice.push(action.payload.price);
        state.total += action.payload.price;
    },

    removeProduct:(state, action)=>{
        if(state.quantity > 0) {
            state.quantity -= 1;
            state.total = state.total - state.shoePrice.pop(1);
            state.products.pop(1);
        }
    },
  },
});

export const {addProduct, removeProduct} = cartSlice.actions
export default cartSlice.reducer;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you want to remove the product from array using index:

state.products.splice(index, 1)

If you want to just pop the last added product:

state.products.splice(state.products.length-1, 1)
about 4 years ago · Juan Pablo Isaza Denunciar
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