ProductCard.jsx์
useContext๋ฅผ ์ฌ์ฉํด์ userContext๋ฅผ ๊ฐ์ ธ์จ๋ค.
userContext์๋ ํ์ฌ ๋ก๊ทธ์ธ ์ค์ธ ์ ์ ์ ์ ๋ณด๊ฐ ๋ด๊ฒจ์๊ณ
const ProductCard = ({ product }) => {
const { addToCart } = useContext(CartContext);
const user = useContext(UserContext);
์ฅ๋ฐ๊ตฌ๋ ๋ฒํผ์ ์กฐ๊ฑด์ ์ถ๊ฐํ์ฌ ์ ์ ๊ฐ ์์ ๊ฒฝ์ฐ์๋ง ๋ ธ์ถํ๊ฒ ํ๋ค.
{/* ์ฌ๊ณ ๊ฐ ์์ ๊ฒฝ์ฐ ๋ฐ ์ ์ ๊ฐ ์์ ๊ฒฝ์ฐ์๋ง ์ฅ๋ฐ๊ตฌ๋ ๋ด๊ธฐ ํ์ */}
{product?.stock > 0 && user && (
<button
className="add_to_cart"
onClick={() => addToCart(product, 1)}
>
<img src={basket} alt="basket button" />
</button>
)}
</footer>
1. ์ ์ ์์ ๊ฒฝ์ฐ

2. ์ ์ ์์ ๊ฒฝ์ฐ

๋ง์ฐฌ๊ฐ์ง๋ก SingleProductPage.jsx์๋
์ปจํ ์คํธ๋ก user๋ฅผ ๊ฐ์ ธ์จ๋ค
const { addToCart } = useContext(CartContext);
const user = useContext(UserContext);
๊ตฌ๋งค๊ฐ์ ๋ฒํผ๊ณผ ์ฅ๋ฐ๊ตฌ๋ ์ถ๊ฐ ๋ฒํผ์ ์ ์ ๊ฐ ์์๊ฒฝ์ฐ์๋ง ๋ ธ์ถ
{user && (
<>
<h2 className="quantity_title">๊ตฌ๋งค๊ฐ์:</h2>
<div className="align_center quantity_input">
<QuantityInput
quantity={quantity}
setQuantity={setQuantity}
stock={product.stock}
/>
</div>
<button
onClick={() => addToCart(product, quantity)}
className="search_button add_cart"
>
์ฅ๋ฐ๊ตฌ๋ ์ถ๊ฐ
</button>
</>
)}
</div>
</>
)}
</section>
);
};
1. ์ ์ ๊ฐ ์์ ๊ฒฝ์ฐ

2. ์ ์ ๊ฐ ์์ ๊ฒฝ์ฐ
