При запросе Key ID в React выдает Undefined
При добавлении товара не отображается его ID в консоли
App.js
if (cartItems.find((item) => Number(item.id) === Number(obj.id))) {
axios.delete(`/cart/${obj.id}`);
setCartItems((prev) => prev.filter((item) => Number(item.id) !== Number(obj.id)));
} else {
axios.post('/cart', obj);
setCartItems((prev) => [...prev, obj]);
}
};
const onRemoveItem = (id) => {
axios.delete(`/cart/${id}`);
setCartItems(prev => prev.filter(item => item.id !== id));
};
const onAddToFavorite = async (obj) => {
try {
if (favorites.find(favObj => favObj.id === obj.id)) {
axios.delete(`/favorites/${obj.id}`);
} else {
const { data } = await axios.post('/favorites', obj);
setFavorites((prev) => [...prev, data]);
}
} catch (error) {
alert('Не удалось добавить в фавориты');
}
};
const onChangeSearchInput = (event) => {
setSearchValue(event.target.value);
};
return (
<div className="page">
{cartOpened && <Page items={cartItems} onCloseCart={() => setCartOpened(false)} onRemove={onRemoveItem} />}
<Header
onClickCart={() => setCartOpened(true)}
/>
<Routes>
<Route path='/'
element={<Home
items={items}
cartItems={cartItems}
searchValue={searchValue}
setSearchValue={setSearchValue}
onChangeSearchInput={onChangeSearchInput}
onAddToCart={onAddToCart}
onAddToFavorite={onAddToFavorite}
/>} />
</Routes>
<Routes>
<Route path='/favorites' element={<Favorites
items={favorites}
onAddToFavorite={onAddToFavorite}
/>} />
</Routes>
</div>
);
}
Card.js
function Card({id, onClickPlus, onFavorite, imageUrl, title, price, favorited=false, added = false}) {
const [isAdded, setIsAdded] = React.useState(added);
const [isFavorite, setIsFavorite] = React.useState(favorited);
const handleClick = () => {
onClickPlus({id, imageUrl, title, price});
setIsAdded(!isAdded);
}
const onClickFavorite = () => {
onFavorite({id, imageUrl, title, price});
setIsFavorite(!isFavorite);
}
return (
<div className="card">
<button className={isFavorite ? "card__button card__button--liked" : "card__button card__button--like"} onClick={onClickFavorite}>
<img className="card__button--size" src={isFavorite ? "/img/favorite.svg" : "/img/like.svg"} alt="heart" />
</button>
<img className="card__photo" src={imageUrl} alt="" />
<h4 className="card__describe">{title}</h4>
<div className="card__footer">
<div className="card__footer2">
<span className="card__price__title">Цена:</span>
<b className="card__price">{price} грн.</b>
</div>
<button className={isAdded ? "card__button active" : "card__button"} onClick={handleClick}>
<img className="card__icon" src={isAdded ? "/img/added.svg" : "/img/plus.svg"} alt="" />
</button>
</div>
</div>);
}
