forked from vercel/next.js
/
Products.tsx
41 lines (38 loc) · 1.08 KB
/
Products.tsx
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
import products from '../data/products'
import { formatCurrencyString } from 'use-shopping-cart'
import { useShoppingCart } from 'use-shopping-cart/react'
const Products = () => {
const { addItem, removeItem } = useShoppingCart()
return (
<section className="products">
{products.map((product) => (
<div key={product.id} className="product">
<img src={product.image} alt={product.name} />
<h2>{product.name}</h2>
<p className="price">
{formatCurrencyString({
value: product.price,
currency: product.currency,
})}
</p>
<button
className="cart-style-background"
onClick={() => {
console.log(product)
addItem(product)
}}
>
Add to cart
</button>
<button
className="cart-style-background"
onClick={() => removeItem(product.id)}
>
Remove
</button>
</div>
))}
</section>
)
}
export default Products