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

124
Views
problem with clearing cart items in Reactjs application

I am new to the Reactjs development side and to understand and have a complete understanding of reactjs i have started working on the ecommerce application. I have finished with the display of products page with the help of .JSON file. So with that i used those id's of the products and i was able add the particular item to the cart and remove them.

The problems I am unable work with are:

  1. remove single item from cart when I have multiple products of similar product.
  2. After adding the necessary products, I want to close the order and clear cart items for the next order but I am not able to do.

Help needed with

  1. I want to clear cart items meanwhile storing the ordered items.
  2. In removing only 1 product at a time when there are similar products with more than 1.

For reference Github link for code: https://github.com/BhupathiVenkataSaiCharan/reactredux/tree/master/src/Redirects/Fvdata

In Fvdata, I have my list of products in data.json In Fvdata > App.js , I have the use of click functions for Adding and removing the cart items.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I check your code and from what I understand you have to tweak these two methods on your App.js file

removeFromCart=(product)=>{
    const cartItems = this.state.cartItems.slice();
    this.setState({
        cartItems: cartItems.filter(x=>x.id !== product.id),
    });
    localStorage.setItem("cartItems",JSON.stringify(cartItems.filter(x=>x.id !==product.id)));
}

clearCart=()=>{
    const cartItems = this.state.cartItems.slice();
    this.setState({
        cartItems: cartItems.filter()
    })
}

into this

removeFromCart=(product)=>{
    const cartItems = this.state.cartItems.slice().map(x => {
           if(x.id === product.id){
            return {
                 ...x,
                 count: x.count -1
               }
           }
         return x
        })filter(x=>x.count > 0);

    this.setState({cartItems});
    localStorage.setItem("cartItems",JSON.stringify(cartItems));
}

clearCart=()=>{
    this.setState({
        cartItems: []
    })
    localStorage.setItem("cartItems",JSON.stringify([]));
}```
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!