Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

460
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!