Search

Props

Props (์†์„ฑ)

๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋กœ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•œ ๋ฐ์ดํ„ฐ properties ์˜ ์ค„์ž„๋ง
์™ธ๋ถ€์—์„œ ์ „๋‹ฌ๋˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜. ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
์ด๊ฒƒ์ด React ์ปดํฌ๋„ŒํŠธ ๊ฐ„์— ์ •๋ณด๋ฅผ ์ฃผ๊ณ ๋ฐ›๋Š” ์ฃผ์š” ๋ฉ”์ปค๋‹ˆ์ฆ˜ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋Š” ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ props๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ณ , ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋Š” ์ด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ™”๋ฉด์— ์ •๋ณด๋ฅผ ํ‘œ์‹œํ•˜๊ฑฐ๋‚˜ ํŠน์ • ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
React์—์„œ๋Š” props๋ฅผ ํ†ตํ•œ ๋‹จ๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ์ด ์ฃผ์š”ํ•œ ํŒจํ„ด์ž…๋‹ˆ๋‹ค.
๋‹จ, ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ Redux, Context API ๋“ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ปดํฌ๋„ŒํŠธ ๊ฐ„์˜ ๋ฐ์ดํ„ฐ ํ๋ฆ„์„ ์กฐ๊ธˆ ๋” ์œ ์—ฐํ•˜๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ธฐ๋Š” ๋ฒ• - ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ props ๋กœ ํ•จ์ˆ˜๋ฅผ ๋‚ด๋ ค์ฃผ๊ณ , ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ ๊ทธ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์‹ค์Šต ์ฝ”๋“œ

โ€ข
์‹ค์Šต1 : โ€œ์ƒํ’ˆ ์ƒ์„ธ UI ๋งŒ๋“ค๊ธฐ - propsโ€
โ€ข
์‹ค์Šต2 : โ€œ์ƒํ’ˆ ๋ชฉ๋ก UI ๋งŒ๋“ค๊ธฐโ€

์‹ค์Šต1

โ€œ์ƒํ’ˆ ์ƒ์„ธ UI ๋งŒ๋“ค๊ธฐ - propsโ€

โ€œ์ƒํ’ˆ ์ƒ์„ธ UI ๋งŒ๋“ค๊ธฐ - propsโ€ ์—์„œ๋Š” ์‚ฌ์ „ ์‹ค์Šต ํ›„ ์ด์–ด์„œ ์‹ค์Šตํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์ „ ์‹ค์Šต - State ์‚ฌ์ „ ์‹ค์Šต์—์„œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ •์˜ํ•œ ์ƒํ’ˆ ์ •๋ณด ๊ฐ์ฒด(product) ๋ฅผ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ •์˜ํ•˜๊ณ  ์ด๋ฅผ props ๋กœ ์ „๋‹ฌํ•˜๋Š” ์‹ค์Šต์„ ํ•ด๋ด…๋‹ˆ๋‹ค.

์‚ฌ์ „ ์‹ค์Šต

์ž‘์—…ํ•  ํŒŒ์ผ

โ€ข
componets/ProductDetail.jsx
โ€ข
App.js

์ปดํฌ๋„ŒํŠธ ๊ตฌ์กฐ

โ€ข
App
โ—ฆ
ProductDetail
App, ProductDetail ์ปดํฌ๋„ŒํŠธ - props ์‚ฌ์šฉ

componets/ProductDetail.jsx

import React, { useState } from 'react' const ProductDetail = ({ product }) => { // โœ… props โžก { product } ๐Ÿ‘ฉโ€๐Ÿซ ๊ฐ์ฒด ๊ตฌ์กฐ๋ถ„ํ•ดํ• ๋‹น // state ์ถ”๊ฐ€ const [quantity, setQuntity] = useState(1) // product{} โžก App.js ์ด๋™ // ์ตœ์ข… ๊ฐ€๊ฒฉ ๊ณ„์‚ฐ const totalPrice = product.price * quantity // ์ด๋ฒคํŠธ ์ •์˜ - ์ฆ๊ฐ€ const onIncrease = () => { console.log(quantity); setQuntity(quantity + 1) } // ์ด๋ฒคํŠธ ์ •์˜ - ๊ฐ์†Œ const onDecrease = () => { console.log(quantity); setQuntity(quantity - 1) } return ( <div className='product-detail'> <div className="item img"> <img src={product.img} alt={product.name} /> </div> <div className="item info"> <div className="title"> <h1>{product.name}</h1> </div> <p> <span className='txt-pt'>INFO</span><br /> -ํŽธ์•ˆํ•œ ์ฐฉ์šฉ์ด ๊ฐ€๋Šฅํ•œ ํด๋ผ ๋””์ž์ธ <br /> -์ฒดํ˜• ์ปค๋ฒ„๊ฐ€ ๊ฐ€๋Šฅํ•œ ๋ฒ ์ด์งํ•œ ์‹ค๋ฃจ์—ฃ <br /> </p> <p> <span className="txt-pt">Color & Size</span> <br /> Black, Navy, Red <br /> 85, 90, 95, 100, 110 <br /> - ์–ด๊นจ 53, ๊ฐ€์Šด 59, ์•”ํ™€ 23, ์†Œ๋งค 62, ์ด์žฅ 68 <br /> (์ธก์ • ๋ฐฉ๋ฒ•์— ๋”ฐ๋ผ 1~3cm ์˜ค์ฐจ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค) <br /> </p> <span className='line-lg'></span> <div className="text-group"> <div className="item"> <span className='txt-pt'>ํŒ๋งค๊ฐ€</span> </div> <div className="item"> <span className='txt-pt'>{product.price.toLocaleString()} ์›</span> </div> </div> <div className="text-group"> <div className="item"> <span>๋ฐฐ์†ก๋น„</span> </div> <div className="item"> <span>3,000</span> ์› </div> </div> <span className="line-lg"></span> <div className="text-group"> <div className="item"> <span>Color</span> </div> <div className="item"> <select name="color" id="color"> <option value="Black">Black</option> <option value="Navy">Navy</option> <option value="Red">Red</option> </select> </div> </div> <div className="text-group"> <div className="item"> <span>Size</span> </div> <div className="item"> <select name="color" id="color"> <option value="85">85</option> <option value="90">90</option> <option value="95">95</option> <option value="100">100</option> <option value="110">110</option> </select> </div> </div> <div className="text-group"> <div className="item"> <span>์ˆ˜๋Ÿ‰</span> </div> <div className="item flex"> <input type="text" className='quantity' value={quantity} min={1} max={100} /> {/* value ๋ฅผ state ๋กœ ๋ณ€๊ฒฝ */} <button className='btn btn-xs' onClick={ onIncrease }>+</button> {/* ์ด๋ฒคํŠธ ์ถ”๊ฐ€ */} <button className='btn btn-xs' onClick={ onDecrease }>-</button> {/* ์ด๋ฒคํŠธ ์ถ”๊ฐ€ */} </div> </div> <span className="line-lg"></span> <div className="text-group"> <div className="item"> <span className='txt-pt'>์ตœ์ข… ๊ฐ€๊ฒฉ</span> </div> <div className="item"> <span className='txt-pt'>{totalPrice.toLocaleString()} ์›</span> </div> </div> <div className="text-group flex gap-1"> <div className="item"> <button className="btn btn-lg">๊ตฌ๋งคํ•˜๊ธฐ</button> </div> <div className="item flex"> <button className="btn btn-lg btn-outline">์žฅ๋ฐ”๊ตฌ๋‹ˆ</button> <button className="btn btn-lg btn-outline">๊ด€์‹ฌ์ƒํ’ˆ</button> </div> </div> </div> </div> ) } export default ProductDetail
JavaScript
๋ณต์‚ฌ

App.js

import logo from './logo.svg'; import './App.css'; import ProductDetail from './components/ProductDetail'; function App() { // โœ… ๊ฐ์ฒด ๊ฐ€์ ธ์˜ด const product = { productId : 'p000001', name : '๋ฒ ์ด์ง ํด๋ผ ๋‹ˆํŠธ', price : 42000, quantity : 1, img: 'https://i.imgur.com/1vpSkbW.png', } return ( <div> <ProductDetail product={product} /> </div> ); } export default App;
JavaScript
๋ณต์‚ฌ

์‹ค์Šต2

โ€œ์ƒํ’ˆ ๋ชฉ๋ก UI ๋งŒ๋“ค๊ธฐโ€