๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[๊ฐœ๋ฐœ] Practice/React

[React] ์ƒํ’ˆ๋ชฉ๋ก Component ๋กœ ๋งŒ๋“ค์–ด ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ฆฌ๊ธฐ

by Connecting-the-dots 2022. 3. 23.
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ก ์‹ค์Šต ํฌ์ธํŠธ!


๐Ÿ’œ ์ƒํ’ˆ๋ชฉ๋ก Component ๋กœ ๋งŒ๋“ค์–ด ๋ฐ์ดํ„ฐ๋ฐ”์ธ๋”ฉํ•˜๊ธฐ

// data.js ํŒŒ์ผ์˜ ์ƒํ’ˆ ๋ฐ์ดํ„ฐ

export default [
    {
      id : 0,
      title : "White and Black",
      content : "Born in France",
      price : 120000
    },
  
    {
      id : 1,
      title : "Red Knit",
      content : "Born in Seoul",
      price : 110000
    },
  
    {
      id : 2,
      title : "Grey Yordan",
      content : "Born in the States",
      price : 130000
    }
  ];

 

import Data from './data.js'; 

function App() {

  let [shoes, setShoes] = useState(Data);
  
  return (
    <div className="App">

      <div className="container">
        <div className="row">
          <Grid shoes={shoes[0]} />
          <Grid shoes={shoes[1]} />
          <Grid shoes={shoes[2]} />
        </div>
      </div>
      
    </div>
  );
}

function Grid(props) {
  return (
    <div className="col-md-4 grid">
      <img src={`https://codingapple1.github.io/shop/shoes${props.shoes.id + 1}.jpg`} />
      <h5>{props.shoes.title}</h5>
      <p>{props.shoes.content} & {props.shoes.price}</p>
    </div>
  )
}
  • data.js ํŒŒ์ผ์—์„œ Data ๋ผ๋Š” ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„์˜จ ์ƒํ’ˆ ๋ฐ์ดํ„ฐ๋Š” shoes ๋ผ๋Š” state ์— ์ €์žฅํ•ด์ฃผ์—ˆ๋‹ค.
  • ์ƒํ’ˆ๋ชฉ๋ก ํ•œ ๊ฐœ๋ฅผ Grid ๋ผ๋Š” Component ๋กœ ๋งŒ๋“ค์–ด์„œ ๊ธฐ์กด์— ์ƒํ’ˆ๋ชฉ๋ก ํƒœ๊ทธ๋“ค์ด ์žˆ๋˜ ์œ„์น˜์— ๊ฐ๊ฐ ๋„ฃ์–ด ์ด 3๊ฐœ์˜ Component ๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.
  • Grid Component ๋Š” ์œ„์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.
    • ๊ฐ Grid Component ๋Š” ๋‹ค๋ฅธ ์ƒํ’ˆ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์™€์•ผ ํ•˜๋ฏ€๋กœ App Component ์˜ ์ฒซ๋ฒˆ์งธ Grid ๋Š” shoes[0] ๋ฐ์ดํ„ฐ๋ฅผ shoes ๋ผ๋Š” ๋ณ€์ˆ˜๋ช…์œผ๋กœ,  ๋‘๋ฒˆ์งธ Grid ๋Š” shoes[1] ๋ฐ์ดํ„ฐ๋ฅผ shoes ๋ผ๋Š” ๋ณ€์ˆ˜๋ช…์œผ๋กœ.. ์ด๋Ÿฐ ์‹์œผ๋กœ Grid Component ๋กœ ์ „์†กํ•ด์ฃผ์—ˆ๋‹ค.
    • ๊ทธ๋Ÿฌ๋ฉด Grid Component ์—์„œ๋Š” props ๋กœ object ํ˜•ํƒœ์ธ shoes ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์•„์„œ ํ•„์š”ํ•œ ์œ„์น˜์— title, content, price ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐ”์ธ๋”ฉ๋˜๋„๋ก ํ•ด์ฃผ์—ˆ๋‹ค.
    • ์ด๋ฏธ์ง€๋ฅผ ๋ฐ์ดํ„ฐ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๊ฒŒ ์กฐ๊ธˆ ์–ด๋ ค์› ๋Š”๋ฐ, ์ด๋ฏธ์ง€ ํŒŒ์ผ ์ด๋ฆ„์ด shoes1.jpg, shoes2.jpg ์ด๋Ÿฐ์‹์œผ๋กœ ๊ฐ ์ธ๋ฑ์Šค์— 1์„ ๋”ํ•œ ์ˆซ์ž๊ฐ€ ๋ถ™์–ด์žˆ์—ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ˆซ์ž๋งŒ ์ˆ˜์ •ํ•ด์ฃผ๋ฉด ๋˜๋ฏ€๋กœ ๋ฐฑํ‹ฑ ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ props.shoes.id +1 ์„ ์ž‘์„ฑํ•˜์—ฌ ํ•ด๋‹น ์ธ๋ฑ์Šค + 1 ํ•œ ์ˆซ์ž๊ฐ€ shoes ๊ธ€์ž ๋’ค์— ๋ถ™๋„๋ก ํ•ด์ฃผ์—ˆ๋‹ค.

 

 


๐Ÿ’œ Component ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ฆฌ๊ธฐ

import Data from './data.js'; 

function App() {

  let [shoes, setShoes] = useState(Data);
  
  return (
    <div className="App">

      <div className="container">
        <div className="row">
          {
            shoes.map((item, idx)=>{
              return (
                <Grid shoes={shoes[idx]} key={idx}/>
              )
            })
          }
        </div>
      </div>
      
    </div>
  );
}

function Grid(props) {
  return (
    <div className="col-md-4 grid">
      <img src={`https://codingapple1.github.io/shop/shoes${props.shoes.id + 1}.jpg`} />
      <h5>{props.shoes.title}</h5>
      <p>{props.shoes.content} & {props.shoes.price}</p>
    </div>
  )
}
  • shoes ๋ผ๋Š” state ์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ์˜ ์š”์†Œ ๊ฐœ์ˆ˜๋งŒํผ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ค์•ผ ํ•˜๋ฏ€๋กœ shoes ์— map() ํ•จ์ˆ˜๋ฅผ ๋ถ™์—ฌ์ฃผ์—ˆ๋‹ค.
  • map() ํ•จ์ˆ˜ ์•ˆ์—๋Š” shoes ์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ ์˜๋ฏธํ•˜๋Š” item ์ด๋ผ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ ๋•Œ๋งˆ๋‹ค ์ฆ๊ฐ€ํ•˜๋Š” ์ •์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋Š” idx ๋ผ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  Grid Component ์•ˆ์˜ shoes ๋ณ€์ˆ˜๋“ค์€ shoes[0], shoes[1], ์ด๋Ÿฐ ์‹์œผ๋กœ ์ธ๋ฑ์Šค๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ ์ˆซ์ž ๋Œ€์‹  idx ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์ด ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก ์ˆ˜์ •ํ•ด์ฃผ์—ˆ๋‹ค.
  • ์—ฌ๊ธฐ๊นŒ์ง€๋งŒ ์ง„ํ–‰ํ•˜๋ฉด React ํ”„๋กœ์ ํŠธ๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ๊ฐœ๋ฐœ์ž๋„๊ตฌ์—์„œ key ๊ฐ’์„ ์ž…๋ ฅํ•˜์ง€ ์•Š์•˜๋‹ค๊ณ  warning ์ด ๋œจ๋Š”๋ฐ, key ๊ฐ’์€ ๊ณ ์œ ํ•œ ๊ฐ’์ด ํ•„์š”ํ•˜๊ณ  idx ๊ฐ€ ์ถฉ๋ถ„ํžˆ ๊ทธ ์—ญํ• ์„ ํ•ด์ฃผ๋ฏ€๋กœ key={idx} ๋กœ ์ž‘์„ฑํ•ด์ฃผ์—ˆ๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•