-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProduct.js
51 lines (43 loc) · 1.16 KB
/
Product.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import React from "react";
import "./Product.css";
import { useStateValue } from "./StateProvider";
function Product({ id,title, image, price, rating }) {
const [{ basket }, dispatch] = useStateValue();
const addToBasket = () => {
//dispatch the item into the data layer
dispatch({
type: "ADD_TO_BASKET",
item: {
id: id,
title: title,
image: image,
price: price,
rating: rating,
},
});
}
return (
<div className="product">
<div className="product_info">
<p>{title}</p>
<p className="product_price">
<small>$</small>
<strong>{price}</strong>
</p>
<div className="product_rating">
{Array(rating)
.fill()
.map((_, i) => (
<p>⭐</p>
))}
</div>
</div>
<img
src={image}
alt=""
/>
<button onClick={addToBasket}>Add to Basket</button>
</div>
)
}
export default Product