ProductCard.jsx
<Link to={`/product/${id}`}>
<img
src={`http://localhost:5000/products/${image}`}
// {image}๋ง ๋ฃ์ผ๋ฉด ์ ๋๋ก ๋ถ๋ฌ์ค์ง ๋ชปํจ
// ๋ฐฑ์๋ ์๋ฒ ์ฃผ์๋ก ์์ฒญํ๋ฉด ๋จ
alt="product image"
/>
</Link>
ํ์ฌ ์ํ ๋ชฉ๋ก์์ ์ํ ํ๋๋ฅผ ํด๋ฆญํ๋ฉด ์์ธํ์ด์ง๋ก ๋์ด๊ฐ๋ค.


๋ผ์ฐํ ์ ์๋์ ๊ฐ์ด id๊ฐ ๋ค์ด๊ฐ๋ฉด ์๋์ ํ์ด์ง(์์ธํ์ด์ง)๋ก ์ด๋
<Route path="/product/:id" element={<SingleProductPage />} />
SingleProductPage.jsx
import { useState } from "react";
import "./SingleProductPage.css";
import QuantityInput from "./QuantityInput";
import { useParams } from "react-router-dom";
import useData from "../../Hook/useData";
const SingleProductPage = () => {
//์ฒ์ ์์ ์ด๋ฏธ์ง ๋ฒํธ๋ 0์ -> product.images[0] = image1 ์ ์๋ฏธํจ
const [selectedImage, setSelectedImage] = useState(0);
const { id } = useParams(); //์ฃผ์ ๋ณ์ path variable๋ฐ๊ธฐ
const { data: product, error, isLoading } = useData(`/products/${id}`); //ํน์ ์์ด๋ ์ ํ๋ง ๊ฐ์ ธ์ค๊ฒํจ
return (
<section className="align_center single_product">
{error && <em className="form_error">{error}</em>}
{product._id && (
<>
<div className="align_center">
<div className="single_product_thumbnails">
{/* product ์์ images๋ฅผ map ๋ฐ๋ณตํ๋ค. */}
{product.images.map((image, index) => (
<img
src={`http://localhost:5000/products/${image}`}
alt={product.title}
// css ๋ selectedImage state๊ฐ index์ผ ๊ฒฝ์ฐ selected_image ์ ์ฉํ๋ค.
className={selectedImage === index ? "selected_image" : ""}
// ํด๋ฆญํ๋ฉด index๋ก ์คํ
์ดํธ ์ ์ฅ
onClick={() => setSelectedImage(index)}
/>
))}
</div>
<img
// ์ด๋ฏธ์ง๋ ์ ํ๋ index์ ์ด๋ฏธ์ง๋ก ์ถ๋ ฅํ๋ค.
src={`http://localhost:5000/products/${product.images[selectedImage]}`}
alt={product.title}
className="single_product_display"
/>
</div>
{/* ์ํ ๋ํ
์ผ */}
<div className="single_product_details">
<h1 className="single_product_title">{product.title}</h1>
<p className="single_product_description">{product.description}</p>
<p className="single_product_price">
{/* ์ฐ๋ฆฌ๋๋ผ ์ํ์ ๋ง๊ฒ toLocaleString์ผ๋ก ๋ณํ */}๏ฟฆ{" "}
{product.price.toLocaleString("ko-KR")} ์
</p>
<h2 className="quantity_title">๊ตฌ๋งค๊ฐ์:</h2>
<div className="align_center quantity_input">
<QuantityInput />
</div>
<button className="search_button add_cart">์ฅ๋ฐ๊ตฌ๋ ์ถ๊ฐ</button>
</div>
</>
)}
</section>
);
};
export default SingleProductPage;
๊ธฐ์กด์ ์๋ const products ๊ฐ์ฒด๋ ์ญ์ ํ๋ค.
๊ทธ๋ฆฌ๊ณ useData๋ก ์์ดํ ํ๋์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋๋ฐ,
์๋ฌ๊ฐ ๋ ๊ฒฝ์ฐ์๋ error์ ๋ด์์ error์ถ๋ ฅ์ ํด์ฃผ๊ณ

product๊ฐ ์์ ๊ฒฝ์ฐ์๋ ๋ฐ๋ณต๋ฌธ์ผ๋ก product์์ ๋ด์ฉ์ ์ถ๋ ฅํ๋ค.
์ด๋ฏธ์ง์ ๊ฒฝ์ฐ ๋ฐฑ์๋ ์ฃผ์๋ฅผ ์ฌ์ฉํด์ผํด์ ์ฃผ์ ์ ์ฒด๋ฅผ ์ ๋ ฅํด์ ๋ฐ์์จ๋ค.

'FRONTEND > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [myCart] ์ํ ์์ธ ํ์ด์ง ์๋ ์ฆ๊ฐ ๋ฒํผ (0) | 2023.12.20 |
|---|---|
| [myCart] ๋ก๋ฉ ์ค ํ๋ฉด ํ์ํ Loader ์ปดํฌ๋ํธ ์์ฑ (1) | 2023.12.20 |
| [myCart] ํ์ด์ง๋ค์ด์ (0) | 2023.12.20 |
| [myCart] ์นดํ ๊ณ ๋ฆฌ ๋ณ ์ํ ์ถ๋ ฅ (0) | 2023.12.20 |
| [myCart] ๋ฆฌ์กํธ ๋ก๋ฉ ์ค์ผ๋ ํค ์ ์ฉ (0) | 2023.12.20 |