Front-end/React

[React] ์ˆœ์ˆ˜ ๋ฆฌ์•กํŠธ (๊ฐ€์ƒ DOM, ๋ฆฌ์•กํŠธ ์—˜๋ฆฌ๋จผํŠธ, ReactDOM)

 

๐Ÿง ๊ฐ€์ƒ DOM์ด๋ž€?

 

  • ์‹ค์ œ DOM์— ์ ‘๊ทผํ•˜๋Š” ๋Œ€์‹ , ์ด๋ฅผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด๋กœ ๊ตฌ์„ฑํ•ด ์ถ”์ƒํ™”ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.
  • ๋ฆฌ์•กํŠธ๊ฐ€ ๋ชจ๋“  ์ฒ˜๋ฆฌ๋ฅผ ๋Œ€์‹  ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์œ„์™€ ๊ฐ™์€ DOM API ์กฐ์ž‘ ์ž‘์—…์„ ์ง์ ‘ ์‹ ๊ฒฝ ์“ธ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.
  • ๋Œ€์‹  ๊ฐ€์ƒ DOM์„ ๋‹ค๋ฃจ๊ฑฐ๋‚˜ ๋ฆฌ์•กํŠธ๊ฐ€ UI๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ธŒ๋ผ์šฐ์ €์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ช‡ ๊ฐ€์ง€ ๋ช…๋ น๋งŒ ๋‹ค๋ฃจ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

๐Ÿง React Element (๋ฆฌ์•กํŠธ ์—˜๋ฆฌ๋จผํŠธ)

  • ๊ฐ€์ƒ DOM์€ ๋ฆฌ์•กํŠธ ์—˜๋ฆฌ๋จผํŠธ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ธŒ๋ผ์šฐ์ € DOM์€ DOM ์—˜๋ฆฌ๋จผํŠธ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ด ๋•Œ, ๋ฆฌ์•กํŠธ ์•จ๋ฆฌ๋จผํŠธ๋Š” ๊ทธ์— ๋Œ€์‘ํ•˜๋Š” ์‹ค์ œ DOM ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ์–ด๋–ป๊ฒŒ ์ƒ๊ฒจ์•ผํ•˜๋Š”์ง€ ๊ธฐ์ˆ ํ•ด์ค๋‹ˆ๋‹ค. (๋ฆฌ์•กํŠธ ์—˜๋ฆฌ๋จผํŠธ -> DOM ์—˜๋ฆฌ๋จผํŠธ)

๋ฆฌ์•กํŠธ ์—˜๋ฆฌ๋จผํŠธ ( React.createElement๋ฅผ ์‚ฌ์šฉํ•ด ์ƒ์„ฑ )

React.createElement("h1", null, "์‹ ์„ ํ•œ ์‚ฌ๊ณผ")

React.createElement

  • 1๋ฒˆ์งธ ์ธ์ž : ๋งŒ๋“ค๋ ค๋Š” ์—˜๋ฆฌ๋จผํŠธ์˜ ํƒ€์ž… ์ •์˜
  • 2๋ฒˆ์งธ ์ธ์ž : ์—˜๋ฆฌ๋จผํŠธ์˜ ํ”„๋กœํผํ‹ฐ
  • 3๋ฒˆ์งธ ์ธ์ž : ์—˜๋ฆฌ๋จผํŠธ์˜ ์ž์‹ ๋…ธ๋“œ

 

DOM ์—˜๋ฆฌ๋จผํŠธ

<h1>Fresh Apple</h1>

๋‘˜์€ ๊ฐ™๋‹ค. ๋ฆฌ์•กํŠธ ์—˜๋ฆฌ๋จผํŠธ๋Š” ๋‹จ์ง€ ๋ฆฌ์•กํŠธ๊ฐ€ DOM ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๋ ค์ฃผ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฆฌํ„ฐ๋Ÿด

 

๐Ÿง React DOM

  • React DOM์ด๋ž€ Virtual DOM์—์„œ HTML์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.
  • React DOM์—๋Š” ๋ฆฌ์•กํŠธ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋ธŒ๋ผ์šฐ์ €์— ๋ Œ๋”๋งํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋ชจ๋“  ๋„๊ตฌ๊ฐ€ ๋“ค์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • render ๋ฉ”์†Œ๋“œ์™€ ์„œ๋ฒ„์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ renderToString๊ณผ renderToStaticMarkup ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

(์˜ˆ์‹œ)

var fruit = React.createElement("h1", null, "์‹ ์„ ํ•œ ์‚ฌ๊ณผ")
ReactDOM.render(fruit, document.getElementById('react-container'))

ReactDOM.render

  • 1๋ฒˆ์งธ ์ธ์ž : ๋ Œ๋”๋งํ•  ๋ฆฌ์•กํŠธ ์—˜๋ฆฌ๋จผํŠธ
  • 2๋ฒˆ์งธ ์ธ์ž : ๋ Œ๋”๋ง์ด ์ผ์–ด๋‚  ๋Œ€์ƒ DOM ๋…ธ๋“œ

 ์•„๋ž˜์™€ ๊ฐ™์ด HTML์— ์ •์˜ํ•ด๋‘” react-container๋ผ๋Š” id๋ฅผ ๊ฐ€์ง„ div์˜ ์ž์‹์œผ๋กœ h1 ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค.

<div id="react-container">
	<h1>์‹ ์„ ํ•œ ์‚ฌ๊ณผ</h1>
</div>

ReactDOM์—์„œ props.children์„ ์‚ฌ์šฉํ•ด ์ž์‹ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋ Œ๋”๋ง ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. -> ์ปดํฌ๋„ŒํŠธ ํŠธ๋ฆฌ

 

(์˜ˆ์‹œ)

React.createElement(
	"ul",
    null,
    React.createElement("li", null, "์‚ฌ๊ณผ 1๊ฐœ"),
    React.createElement("li", null, "ํฌ๋„ 2๊ฐœ"),
    React.createElement("li", null, "๋ฉœ๋ก  5๊ฐœ"),
)

4๋ฒˆ์งธ ์ดํ›„์— ์ถ”๊ฐ€๋œ ์ธ์ž๋Š” ๋‹ค๋ฅธ ์ž์‹ ์—˜๋ฆฌ๋จผํŠธ๋กœ ์ทจ๊ธ‰๋ฉ๋‹ˆ๋‹ค.

<ul>
    <li>์‚ฌ๊ณผ 1๊ฐœ</li>
    <li>ํฌ๋„ 2๊ฐœ</li>
    <li>๋ฉœ๋ก  5๊ฐœ</li>
</ul>

 

์•„๋ž˜์™€ ๊ฐ™์ด UI์—˜๋ฆฌ๋จผํŠธ์™€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฆฌ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. -> ๋ฆฌ์•กํŠธ์˜ ๊ฐ€์žฅ ํฐ ์žฅ์ 

let items = {
	"์‚ฌ๊ณผ 1๊ฐœ",
	"ํฌ๋„ 2๊ฐœ",
	"๋ฉœ๋ก  5๊ฐœ"
}
React.createElement("ul", {className : "ingredients"},
	items.map((ingredient, i) => 
		React.createElement("li", {key:i}, ingredient))
)

 

๋ฐ˜์‘ํ˜•