• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

What different ways can you update and remove the item from the State in ReactJS?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
handleAddToCart method updates the cartItem ;




handleRemoveFromCart updates the cartItem




Both works fine, but what would anyone do to achieve this in different way?
 
Sheriff
Posts: 67585
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wouldn't use reduce for the removal. It'd be much more conventional to use filter.
 
manoj karmocha
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:I wouldn't use reduce for the removal. It'd be much more conventional to use filter.



But how to add extra condition while using filter?
 
Bear Bibeault
Sheriff
Posts: 67585
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd break that out into a separate operation as it is a separate concern. Simple code with good clarity is more important that micro-optimizations.
 
manoj karmocha
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:I'd break that out into a separate operation as it is a separate concern. Simple code with good clarity is more important that micro-optimizations.



can you please show how can i achieve that
 
Bear Bibeault
Sheriff
Posts: 67585
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I'm understanding what you need to do:

1) Find the item and subtract 1 from the quantity (.find() useful here)

2) Filter the cart so that anything with 0 quantity is removed (.filter() useful here)

This keeps the operations separate and each performs one concern. This improves clarity of the code, and, if you're testing, each operation can be separately tested.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic