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

126
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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