Front-end/React

[React] ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ (ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์™€ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ)

๐Ÿง ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ

  • ๋ฆฌ์•กํŠธ์—์„œ ์œ„์™€ ๊ฐ™์€ ๊ฐ ๋ถ€๋ถ„๋“ค์„ ์ปดํฌ๋„ŒํŠธ๋ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์ง‘ํ•ฉ์— ๊ฐ™์€ DOM ๊ตฌ์กฐ๋ฅผ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฆฌ์•กํŠธ๋กœ ๋งŒ๋“ค๊ณ  ์‹ถ์€ UI๋ฅผ ์ƒ๊ฐํ•  ๋•Œ, ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์žฌ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๊ณ ๋ คํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋Ÿฐ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ™•์žฅ์„ฑ์ด ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

๋ฆฌ์•กํŠธ์—์„œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“œ๋Š” 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•

  • createClass๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
  • ES6 Class๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
  • ์ƒํƒœ๊ฐ€ ์—†๋Š” ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

1) React.createClass (createClass๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•)

  • ๋ฆฌ์•กํŠธ๊ฐ€ ์ฒ˜์Œ ๋งŒ๋“ค์–ด์กŒ์„ ๋•Œ์˜ ์œ ์ผํ•œ ๋ฐฉ๋ฒ•์ด์—ˆ๋‹ค.
  • ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์•„์ดํ…œ๋“ค์ด ๋“ค์–ด ์žˆ๋Š” ๋ฐฐ์—ด์„ ์ปดํฌ๋„ŒํŠธ์— ํ”„๋กœํผํ‹ฐ๋ฅผ ํ†ตํ•ด ๋„˜๊ธธ ์ˆ˜ ์žˆ๋‹ค.

(์˜ˆ์‹œ)

const FruitList = React.createClass({
	displayName: "FruitList"
    render(){
    return React.createElement("ul", {className: "fruits"},
                              this.props.items.map((fruit, i) => React.createElement("li", {key: i}, fruit)
                                                  )
                              )
	}
})

const items = {
    "์‚ฌ๊ณผ 1๊ฐœ",
    "ํฌ๋„ 2๊ฐœ",
    "๋ฉœ๋ก  5๊ฐœ"
}

ReactDOM.render(
	React.createElement(FruitsList, {item}, null),
    document.getElementById('react-container')
)
<FruitsList items =[...]>
	<ul classname = "fruits">
        <li key="0">์‚ฌ๊ณผ 1๊ฐœ</li>
        <li key="1">ํฌ๋„ 2๊ฐœ</li>
        <li key="2">๋ฉœ๋ก  5๊ฐœ</li>
    </ul>
</FruitsList>
  • ์ปดํฌ๋„ŒํŠธ๋Š” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ผ๋ฐ˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํด๋ž˜์Šค์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋‚ด๋ถ€์— ์ฝ”๋“œ๋ฅผ ์บก์Šํ™” ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•ด ๋งŒ๋“ค์–ด์ง„ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์“ธ ๋•Œ๋Š” ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๋ผ ์ปดํฌ๋„ŒํŠธ ํด๋ž˜์Šค๋ฅผ ์ง์ ‘ ์“ด๋‹ค๋Š” ์  ์œ ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

2) React.Component (ES6 ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•)

  • ES6์˜ ํด๋ž˜์Šค ์„ ์–ธ์ž…๋‹ˆ๋‹ค.
  • ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค ๋•Œ React.Component๋ฅผ ์ถ”์ƒ ํด๋ž˜์Šค๋กœ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ES6 ๊ตฌ๋ฌธ์œผ๋กœ ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•˜๋ฉด ์ปค์Šคํ…€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

(์˜ˆ์‹œ)

class FruitsList extends React.Component {
    renderListItem(ingredient, i){
        return React.createElement("li", { key: i}, fruit)
    }
    render(){
        return React.createElement("ul", {className: "fruits"},
                                  	this.props.items.map(this.renderListItem)
                                  )
    }
}

 

3) ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ

  • ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ์›์น™์„ ์—ฐ์Šตํ•˜๊ธฐ ์ข‹์€ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. -> ์ˆœ์ˆ˜ ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด์ฃผ์ž
  • ํ”„๋กœํผํ‹ฐ๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„์„œ DOM ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • ์ฝ”๋“œ๊ฐ€ ๋” ๋‹จ์ˆœํ•ด์ง€๊ณ , ์ฝ”๋“œ๋ฒ ์ด์Šค๋ฅผ ํ›จ์”ฌ ๋” ์ž˜ ํ…Œ์ŠคํŠธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„ํ‚คํ…์ณ๋ฅผ ๋‹จ์ˆœํ•˜๊ฒŒ ์œ ์ง€ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋‹ค๋ฅธ ์ปดํฐ๋„ˆํŠธ ๋ฐฉ๋ฒ•๋ณด๋‹ค ์„ฑ๋Šฅ์ด ์šฐ์ˆ˜ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์บก์Šํ™”ํ•ด์•ผ ํ•˜๊ฑฐ๋‚˜ this๊ฐ€ ํ•„์š”ํ•˜๋ฉด ์‚ฌ์šฉ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  • state๋ฅผ ๊ฐ–์ง€ ๋ชปํ•˜๋ฉฐ, life-cycle ํ•จ์ˆ˜ ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

(์˜ˆ์‹œ)

const fruitsList = props =>
	React.createElement("ul", {className: "fruits"},
                       props.items.map((fruit, i) =>
                                      React.createElement("li", {key: i}, fruit)
                                      )
                       )

 

ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ VS ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ

  • ํ•จ์ˆ˜ํ˜•์˜ ์ฝ”๋“œ๊ฐ€ ๋” ๋‹จ์ˆœํ•˜๋ฉฐ ๊ฐ„๊ฒฐํ•˜๋‹ค. ์†๋„๋„ ๋” ๋น ๋ฅด๋‹ค
  • ํ•˜์ง€๋งŒ ํ•จ์ˆ˜ํ˜•์€ state, this, life-cycle ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์—†๋‹ค.

 

๐Ÿ‘ ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹์— ๋งž๊ฒŒ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋ฅผ ์จ์ฃผ์ž!

( state์™€ this ๊ทธ๋ฆฌ๊ณ  life-cycle ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด )

๋ฐ˜์‘ํ˜•