Reinier Kaper
Reinier Kaper

Reputation: 21

React: store items in a cart with a unique ID

We're building a store in React and I'm running into issues with removing items from the cart.

Example: if we add product "foobar" to the cart multiple times, they will all be references to the original "foobar" product.

For reasons too long to explain, we don't want to keep track of quantities, so I need unique identifiers per cart item (not per product) so I can remove them easily from the cart.

I was thinking of using the Unix timestamp as identifier, but I feel there must be a cleaner solution.

Upvotes: 1

Views: 522

Answers (1)

ykravv
ykravv

Reputation: 291

Consider generating uniq id for newly created cart item. Its id should not be id of product.

There are many ways to generate such id's: one, two

Upvotes: 2

Related Questions