Session 12. Reusable Component (map, props, mockup data)

๊น€๋ฏผ์žฌยท2021๋…„ 9์›” 2์ผ
0

TIL, WeCode, Courseย 

๋ชฉ๋ก ๋ณด๊ธฐ
28/48
post-thumbnail

*๐Ÿ”Study Keyword :

  • map, mockup data๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ์— ๐Ÿ”‘props์— ์ „๋‹ฌํ•˜๋Š” ๋ฒ•์— ๋Œ€ํ•ด์„œ!

map์„ ์•Œ์•„์•ผํ•˜๋Š” ์ด์œ ?!

  • map ๋ฉ”์†Œ๋“œ๋Š” ๋ฐฐ์—ด ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ๋กœ ์ƒˆ๋กœ ๋งคํ•‘๋œ ์ƒˆ๋กœ์šด ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ๊ฒƒ์ด ํŠน์ง•์ด๋‹ค.

  • ์œ„์˜ ์ด๋ฏธ์ง€๋Š” ํ•˜๋‚˜์˜ ๊ฐ™์€ ํ˜•์‹์„ ๊ฐ€์ง„ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฐ˜๋ณต๋œ ํ˜•ํƒœ๋กœ์„œ ์•„๋ž˜์˜ mock data๋กœ ๋ถ€ํ„ฐ ์–ป์€ ์ฃผ์†Œ ๊ฐ’, ์ƒํ’ˆ๋ช…, ๊ฐ€๊ฒฉ์„ ๋ฐ์ดํ„ฐ๋กœ ๋ฐ›์•„ map ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์ƒˆ๋กœ์šด ์ปดํฌ๋„ŒํŠธ ํ˜•ํƒœ 3๊ฐœ๋กœ ๋ฆฌํ„ดํ•œ ๊ฒฐ๊ณผ์ด๋‹ค.

mockup data?

  • mockup data๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” api ๊ตฌ์ถ• ์ „ ์ž„์‹œ ๋ฐฉํŽธ์œผ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐ์ดํ„ฐ๋กœ ํ™”๋ฉด์— ๊ทธ๋ ค๋ณด๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋˜๊ณค ํ•œ๋‹ค.
<script>
const DATA = [
	{
		id: 1,
		img: "https://s3.ap-northeast-2.amazonaws.com/cdn.wecode.co.kr/social/mushroom.jpg",
		name: "๋ง›ํƒ€๋ฆฌ ๋ฒ„์„ฏ 200g",
		price: "1,000์›"
	}, 
	{
		id: 2,
		img: "https://s3.ap-northeast-2.amazonaws.com/cdn.wecode.co.kr/social/octo.jpg",
		name: "[์€ํ•˜์ˆ˜์‚ฐ] ๋ชจ๋ฆฌํƒ€๋‹ˆ์‚ฐ ์ž์ˆ™๋ฌธ์–ด ํ•œ๋งˆ๋ฆฌ(๋ƒ‰๋™)",
		price: "27,000์›"
	}, 
	{
		id: 3,
		img: "https://s3.ap-northeast-2.amazonaws.com/cdn.wecode.co.kr/social/pota.jpg",
		name: "[KF365] ๋ฐค๊ณ ๊ตฌ๋งˆ 800g/๋ด‰",
		price: "4,590์›"
	}, 
	{
		id: 4,
		img: "https://s3.ap-northeast-2.amazonaws.com/cdn.wecode.co.kr/social/vegi.jpg",
		name: "๊ฒฝ๊ธฐ ์‹œ๊ธˆ์น˜ 250g/๋‹จ",
		price: "2,450์›"
	}
]
export default DATA;
</script>

Props

-์œ„ ์ด๋ฏธ์ง€์—์„œ๋Š” LoginPage ์ปดํฌ๋„ŒํŠธ์—์„œ Button ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค.

  • LoginPage๊ฐ€ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ
  • Button์ด ์ž์‹ ์ปดํฌ๋„ŒํŠธ
  • Props๋ฅผ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ๋„˜๊ธธ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋ ‡๊ฒŒ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€๋‹Œ ๊ฐ์ฒด๋ฅผ Props๋ผํ•œ๋‹ค.
  • ๋”ฐ๋ผ์„œ props๋ฅผ ํ†ตํ•ด ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋Š” ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ๋ฐ›์•„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
    • EX>๋™์ผํ•œ ์ปดํฌ๋„ŒํŠธ์— ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ ํ•  ์ˆ˜ ์žˆ๋‹ค

-๋ฆฌ์•กํŠธ๋ฅผ JSX ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์— ๋Œ€ํ•ด์„œ ๋ช…์‹ฌํ•˜์ž!

  • JSX๋ž€ HTML ์•ˆ์—์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ ์“ฐ๋Š” ๋ฌธ๋ฒ•์ธ๋ฐ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์— Props ์ „๋‹ฌ์‹œ {}๋กœ ํ•ญ์ƒ ๋ฌถ์–ด์ค€๋‹ค
    • EX> <LoginButton abc={{abc:1}}/>
    • jsx ๋ฌธ๋ฒ•์„ ์จ์„œ ๊ฐ์ฒด๋ฅผ ๋‹ด์€ ํ˜•ํƒœ
    • EX2> <LoginButtont text={'ํ™•์ธ'}/>
    • ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ ์ปดํฌ๋„ŒํŠธ์— Props๋กœ 'text' ํ‚ค์™€ 'ํ™•์ธ' ํ‚ค์˜ ๊ฐ’์ด ๋„˜์–ด๊ฐ„ ํ˜•ํƒœ๋กœ ๊ฐ์ฒด์˜ ํ˜•ํƒœ๋กœ ๊ฐ’์„ ๋„˜๊ฒจ๋ฐ›์•˜๋‹ค.
  • ProductCard ์ปดํฌ๋„ŒํŠธ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ App, ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์„ props๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.
  • ๋ Œ๋”ํ•จ์ˆ˜๊ฐ€ ํ•ญ์ƒ ์‹คํ–‰์ด๋˜๋ฉด์„œ ๊ทธ๊ฒƒ์˜ ๊ฒฐ๊ณผ๊ฐ€ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚˜์ง€๊ธฐ ๋•Œ๋ฌธ์— ๋ Œ๋” ํ•จ์ˆ˜์™€ ๋ฆฌํ„ด ์‚ฌ์ด์—์„œ ๊ตฌํ˜„ํ•˜๊ณ ์ž ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผํ•œ๋‹ค.
  • ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ž์‹์ปดํฌ๋„ŒํŠธ์— ์ „๋‹ฌํ•œ Props์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” this.props๋กœ ์ ‘๊ทผํ•œ๋‹ค.
  • ์ปดํฌ๋„ŒํŠธ๋Š” ๋…๋ฆฝ์ ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” Props์— ๋”ฐ๋ผ ๊ฐ™์€ ์ปดํฌ๋„ŒํŠธ๋ผ๋„ ๋‹ค๋ฅธ ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
<script>
import React, { Component } from 'react';
class ProductCard extends Component {
  render() {
      console.log(this.props) 
    return (
          <div className="card">
            <img className="productImg" src={this.props.img}/>
            <div className="productInfoWrapper">
              <p className="productName">{this.props.name}</p>
              <p className="productPrice">{this.props.price}</p>
            </div>
          </div>
    )
  }
}
export default ProductCard;
</script>
  • map ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์ž์‹ ์ปดํฌ๋„ŒํŠธ์ธ ProductCard๋ฅผ mock-data์˜ ๊ธธ์ด๋งŒํผ ๋งŒ๋“ค๊ณ  props๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ๋ฐ์ดํ„ฐ๊ฐ€ ์ „๋‹ฌ๋œ ํ˜•ํƒœ์˜ ์ปดํฌ๋„ŒํŠธ๋“ค์„ ๋ฐ˜ํ™˜ํ•˜์—ฌ ํ™”๋ฉด์— ๋ Œ๋”ํ•ด์ค€๋‹ค..!
<script>
import React, {Component} from 'react';
import ProductCard from './Card'
import './App.css'
import DATA from './data';
class App extends Component {
	render() {
	  return (
      <div className="contaniner">
        <div className="titleWrapper">
          <p className="title">๊ธฐ๋ถ„์ข‹์€ JAMIE MARKET</p>
          <p className="subTitle">์ดˆํŠน๊ธ‰ ๋ฐฐ์†ก JAMIE ROCEKT DELIVERY</p>
        </div>
        <div className="cardWrapper">
              {DATA.map((product,idx) => {
                return (
                <ProductCard 
                key={product.id} 
                img={product.img} 
                name={product.name} 
                price={product.price} />
                })
               }
             )
        </div>
      </div>
	  )
	}
};
export default App;
</script>
  • ํ™”๋ฉด์— ๋ Œ๋”ํ•œ ๊ฒฐ๊ณผ

*๐Ÿ’กconclusion

  • props๋Š” ๊ฐ์ฒด๋ผ๋Š” ์‚ฌ์‹ค์„ ์žŠ์ง€๋ง๊ณ  ๋ฆฌ์•กํŠธ์—์„œ props๋ฅผ ์ž˜ ํ™œ์šฉํ•ด๋ณด์ž

# ๐Ÿ“‘Study Source

  • ๋ฐฑ์‹  ๋งž๊ณ  ํšŒ๋ณต์ค‘์ด์‹  ๊ด€ํฌ๋‹˜์˜ ํˆฌํ˜ผ ๊ฐ•์˜ ์ค‘:)
profile
์ž๊ธฐ ์‹ ๋ขฐ์˜ ํž˜์„ ๋ฏฟ๊ณ  ์‹ค์ฒœํ•˜๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๊ณ ์žํ•ฉ๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€