I’m working on an ecommerce project and I’m trying to put all the product selected in the cart dropDown thats shows up in the icon. I did the key id
but it still ask me to give an id to each child?
Where exactly am I wrong?
import React from "react";
import { connect } from "react-redux";
import CustomButton from "../costum-button/costum-button.cpn";
import CartItem from "../cart-item/cart-item.cpn";
import "./cart-dropdown.styles.scss";
const CartDropDown = ({ cartItems }) => (
<div className="cart-dropdown">
<div className="cart-items">
{cartItems.map((cartItem) => (
<CartItem key={cartItem.id} item={cartItem}/>
))}
</div>
<CustomButton>Go to checkout</CustomButton>
</div>
);
const mapStateToProps = ({ cart: { cartItems } }) => ({
cartItems
});
export default connect(mapStateToProps)(CartDropDown);
Cart item
import React from "react";
import "./cart-item.style.scss";
const CartItem = ({ item: { imageUrl, price, name, quantity } }) => (
<div className="cart-item">
<img src={imageUrl} alt="item" />
<div className="item-details">
<span className="name">{name}</span>
<span className="price">
{quantity} x ${price}
</span>
</div>
</div>
);
export default CartItem;
2
Answers
The id’s in cart item must not be unique if it’s still giving you that answer. Although it’s not best to use this, you can always add the index into the key. It’s not best practice to add the index if that list is going to change.
It could be because id of your data is not unique.
Please try with index value of map, and if there are no error, then it must unique problem of id and please check array data.