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
볡μ¬





