๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“— React

[ React ] JSX ์ดํ•ดํ•˜๊ธฐ & Proptypes

by Tamii 2021. 3. 16.
๋ฐ˜์‘ํ˜•

component 

HTML ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ 

component๋ฅผ ์‚ฌ์šฉํ•ด์„œ 

 

ReactDOM.render(
  <App />,
  document.getElementById('root')
);

 

JSX 

javascrtip ์•ˆ์˜ HTML

Component๋ฅผ ๋งŒ๋“ค๊ณ  ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€

<APP />

javascript + HTML์˜ ์กฐํ•ฉ

 

 

 

 

React ์˜ component ์ƒ์„ฑ ํ๋ฆ„

react application์€ ์˜ค์ง ํ•˜๋‚˜์˜ component๋งŒ rendering

๋”ฐ๋ผ์„œ -> ๋ชจ๋“  ๊ฒƒ์€ apapplication(API) ์•ˆ์— ๋“ค์–ด๊ฐ€์•ผํ•จ 

 

๊ตฌํ˜„ํ•˜๊ณ ์‹ถ์€ component ํŒŒ์ผ๋“ค์„ ๋งŒ๋“ค์–ด API.js์— import ํ•˜๊ณ  

API.js๋ฅผ index ์— ๋ Œ๋”๋งํ•˜๋Š” ๋ฐฉ์‹ 

 

 


REACT์˜ ์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ component ๋ถˆ๋Ÿฌ์˜ค๊ธฐ 

โ– ์ •์ ๋ฐ์ดํ„ฐ ์„ค์ • (์ง์ ‘ ๋„ฃ์–ด์ค€ ๋ฐ์ดํ„ฐ) 

ํ•˜์ง€๋งŒ ์‹ค์ œ ์›น์‚ฌ์ดํŠธ์—์„œ๋Š” ๊ธ€์”จ๋ฅผ ์ผ์ผํžˆ ์น  ์ˆ˜ ์—†๋‹ค. json data๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

 

component ์˜ props(์†์„ฑ)์„ ๊ฐ€์ง€๊ณ  ์™€์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

ex) Food์˜ props ์ค‘ fav ๋ฅผ ๊ฐ€์ ธ์˜ด  

 

props.fav  -> { fav }

์ฒ˜์Œ๋ถ€ํ„ฐ fav๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Œ

 


โ– ๋™์  ๋ฐ์ดํ„ฐ ์„ค์ • ( json data )

 

 

* map ํ•จ์ˆ˜ 

A.map

 

๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋ฅผ ๋Œ๋ฉฐ callback ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ณ   array ๊ฒฐ๊ณผ๋ฌผ์„ ๋งŒ๋“ฆ

 

react d์˜ ๋ชจ๋“  element ๋Š” ๊ฐœ๋ณ„์†์„ฑ์„ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค - -> id ์ง€์ •

 

//json data
const foodILike = [
    { 
      id:1,
      name:"waffle",
      image:"https://storcpdkenticomedia.blob.core.windows.net/media/recipemanagementsystem/media/recipe-media-files/recipes/retail/x17/2020_belgian-style-waffles_16700_600x600.jpg?ext=.jpg"
    },
    {
      id:2,
      name:"happy",
      image:"https://storcpdkenticomedia.blob.core.windows.net/media/recipemanagementsystem/media/recipe-media-files/recipes/retail/x17/2020_belgian-style-waffles_16700_600x600.jpg?ext=.jpg",
    }
  
]

//html ์ƒ์„ฑ
function Food({name,pic}){

  return (
    <div>
      <h2>i love {name}</h2>
      <img src={pic} alt={name}></img>
    </div>
  );
}



//json data๋ฅผ ๋Œ๋ฉฐ ๋ฐ˜๋ณต์ ์œผ๋กœ html ์ƒ์„ฑ
function App() {
  return (
    <div>
      {foodILike.map(dish =>(
        <Food key ={dish.id} pic={dish.image}/>
      ))}

    </div>
  );
}

export default App;

ํ•จ์ˆ˜๋กœ ๋นผ์ค˜๋„ ๊ฐ€๋Šฅ

//html ์ƒ์„ฑ
function Food({name,pic}){

  return (
    <div>
      <h2>i love {name}</h2>
      <img src={pic} alt={name}></img>
    </div>
  );
}


//component ์†์„ฑ ๊ฐ€์ ธ์˜ค๊ธฐ
function renderFood(dish){
  return <Food name={dish.name} pic={dish.image}/>
}



//json data๋ฅผ ๋Œ๋ฉฐ ๋ฐ˜๋ณต์ ์œผ๋กœ html ์ƒ์„ฑ (renderFood)ํ•จ์ˆ˜ ์ด์šฉ
function App() {
  return (
    <div>
      {foodILike.map(renderFood) }
    </div>
  );
}

export default App;

 

 

ํ•˜์ง€๋งŒ 

<Food key ={dish.id} pic={dish.image}/>

dish.image๋ฅผ ์‹ค์ˆ˜๋กœ ๋ถ€๋ฅด์ง€ ์•Š์•˜๋‹ค๋ฉด?

dish.id ๊ฐ€ ์—†์—‡๋‹ค๋ฉด? 

์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธฐ๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.


Proptypes ( props ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ )

์ •ํ™•ํ•œ prop์„ ์ „๋‹ฌํ•˜๋Š”์ง€ ํ™•์ธ

 

 

1) prop-types ์„ค์น˜

์˜ฌ๋ฐ”๋ฅธ props ๊ฐ€ ์ „๋‹ฌ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ

npm i prop-types

 

2) Component ์š”์†Œ ๋ฐ‘์— protoTypes ๋กœ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ 

Food.propTypes = {
  name:PropTypes.string.isRequired,
  pic:PropTypes.string.isRequired,
  rating:PropTypes.number.isRequired

}

โ– propTypes  

: ์ด ์ด๋ฆ„์€ ๊ทธ๋Œ€๋กœ ์ง€์ •ํ•ด์•ผ ํ•จ  ex) tamiTypes (X) 

 

โ– isReqired : ํ•„์ˆ˜ ์š”์†Œ

โ– number, string : ์š”์†Œ ์˜ ์˜ ํ˜•ํƒœ 

 

๊ทธ์™ธ ๋‹ค์–‘ํ•œ propTypes๋ฅผ ํ†ตํ•ด ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜๋“ค์ด ์žˆ์Œ

ko.reactjs.org/docs/typechecking-with-proptypes.html

 

 

 

์™„์„ฑ๋œ ์˜ˆ์ œ 

 

 

 

 

 

 

ํ•˜์ง€๋งŒ ์ด ์˜ˆ์ œ๋Š” ํ•œ๊ณ„์ ์ด ์žˆ๋‹ค.

jsondata ๋ฅผ ์ง์ ‘ ํ•˜๋“œ์ฝ”๋”ฉ ํ•œ ๊ฒƒ 

 

 

์‹ค์ œ๋กœ๋Š” ํ•˜๋“œ์ฝ”๋”ฉํ•˜์ง€ ์•Š๊ณ  

๋™์ ์ธ data(dynamic data)๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž‘์—…ํ•œ๋‹ค 

 

๋‹ค์Œ์—” ๋™์  data๋ฅผ ํ™œ์šฉํ•˜๋Š” ์˜ˆ์ œ๋ฅผ  ๊ตฌํ˜„ํ•ด๋ณด์ž

๋Œ“๊ธ€