CS ๊ณต๋ถ€ - front

[React] ๊ฐ€์ƒ๋”

Nuri-KSLV-II 2023. 2. 15. 18:49

๐Ÿ“Œ๊ฐ€์ƒ๋”์˜ ๊ฐœ๋…

- ๊ณต์‹๋ฌธ์„œ ์ •์˜

The virtual DOM (VDOM) is a programming concept where an ideal, or โ€œvirtualโ€, representation of a UI is kept in memory and synced with the โ€œrealโ€ DOM by a library such as ReactDOM. This process is called reconciliation.


๊ฐ€์ƒ์œผ๋กœ UI๋ฅผ ๋ฒ„์ธ„์–ผ๋”์— ์ €์žฅํ•ด๋‘์—ˆ๋‹ค๊ณ  Real Dom๊ณผ ์—ฐ๋™(sync) ์‹œ์ผœ์ฃผ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ปจ์…‰
์ผ๋ช… reconciliation(ํ™”ํ•ด) ๊ณผ์ •์ด๋ผ ์ •์˜

๐Ÿ“ŒDiffing ์•Œ๊ณ ๋ฆฌ์ฆ˜

- ๋ฆฌ์•กํŠธ๋Š” Real Dom๊ณผ Virtual Dom์„ ์ฐธ๊ณ ํ•˜๋ฉด์„œ diffing ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ด ๋ณ€ํ™”๊ฐ€ ์ผ์–ด๋‚œ DOM ์š”์†Œ๋งŒ ์ƒˆ๋กœ ๋ Œ๋”๋งํ•จ

  1. ๋ณ€ํ™”๊ฐ€ ์ผ์–ด๋‚จ State Change - ์ „์ฒด UI๋ฅผ ๊ฐ€์ƒ๋”์— ๋ฆฌ๋ Œ๋”๋ง
  2. ๋ณ€ํ™”์ „์˜ ๊ฐ€์ƒ๋”๊ณผ ๋ณ€ํ™”๋œ ๊ฐ€์ƒ๋” ๋น„๊ต - Compute Diff
  3. ๋ฐ”๋€ ๋ถ€๋ถ„๋งŒ Real Dom ๋ณ€๊ฒฝ(๋นจ๊ฐ„ ์˜์—ญ์—์„œ ๋ ˆ์ด์•„์›ƒ ๋ณ€๊ฒฝ์ด ์ผ์–ด๋‚ฌ๋‹ค๋ฉด, ์‹ค์ œ dom์—์„œ๋Š” ๊ฐ€์ƒ๋” ๋•๋ถ„์— ๋ ˆ์ด์•„์›ƒ ๊ณ„์‚ฐ์ด ์ผ๊ด„๋กœ ๋‹จ ํ•œ๋ฒˆ ์ดํ–‰)

๐Ÿ“Œ์˜ˆ์‹œ

1. element ๋ณ€๊ฒฝ

//๋ณ€๊ฒฝ ์ „
<div>
  <Counter/>
</div>

//๋ณ€๊ฒฝ ํ›„
<span>
  <Counter/>
</span>
  • ์ƒ์œ„ํƒœ๊ทธ๊ฐ€ div์—์„œ span์œผ๋กœ ๋ฐ”๋€Œ๊ฒŒ ๋˜๋ฉด ํ•˜์œ„ Counter ์ปดํฌ๋„ŒํŠธ ์ž์ฒด๊ฐ€ ๋ฐ”๋€Œ์ง€ ์•Š๋”๋ผ๋„ ๋ฆฌ์•กํŠธ๋Š” ๋ชจ๋‘ ๊ฐˆ์•„์—Ž๊ณ  ์ƒˆ๋กœ์šด Dom์„ ์ƒ์„ฑ

2. attribute ๋ณ€๊ฒฝ

//๋ณ€๊ฒฝ ์ „
<div className="before" title="example" />

//๋ณ€๊ฒฝ ํ›„
<div className="after" title="example" />
  • ๊ฐ€์ƒ๋”์„ ํ™œ์šฉํ•ด Dom ์€ ์œ ์ง€์‹œํ‚ค๊ณ , className ์†์„ฑ๋งŒ ๋ฐ”๊ฟˆ, ํšจ์œจ์ ์œผ๋กœ ๋ Œ๋”๋ง ๊ฐ€๋Šฅ

3. ํ•˜์œ„ ์š”์†Œ ์ถ”๊ฐ€

//๋ณ€๊ฒฝ์ „
<ul>
  <li>first</li>
  <li>second</li>
</ul>

//๋ณ€๊ฒฝํ›„
<ul>
  <li>first</li>
  <li>second</li>
  <li>third</li> //์—ฌ๊ธฐ๋งŒ ์ถ”๊ฐ€.
</ul>
  • diffing ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ฒซ๋ฒˆ์งธ, ๋‘๋ฒˆ์งธ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๊ฐ™์€ ๊ฒƒ์„ ํ™•์ธํ•˜๊ณ  ์„ธ๋ฒˆ์งธ๊ฐ€ ๋‹ค๋ฅธ๊ฑธ ํ™•์ธํ•˜์˜€๊ธฐ ๋–„๋ฌธ์— ์„ธ๋ฒˆ์งธ๋งŒ ์ถ”๊ฐ€

3-1. ์˜ˆ์™ธ

//๋ณ€๊ฒฝ์ „
<ul>
  <li>first</li>
  <li>second</li>
</ul>

//๋ณ€๊ฒฝํ›„
<ul>
  <li>๋‚˜Firstํ• ๊ฑฐ์•ผ!</li> //์—ฌ๊ธฐ๋งŒ ์ถ”๊ฐ€๋˜์—ˆ์ง€๋งŒ ์ฒซ๋ฒˆ์งธ ์ˆœ์„œ
  <li>first</li>
  <li>second</li>
</ul>
  • ๋ฆฌ์•กํŠธ๊ฐ€ ์ฒซ๋ฒˆ์งธ๋งŒ ํŒŒ์•…ํ•ด์„œ ์ถ”๊ฐ€ํ•˜๊ธฐ๋ฅผ ๊ธฐ๋Œ€ํ•˜๊ฒ ์ง€๋งŒ, ์ฒซ๋ฒˆ์งธ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋ฐ”๋€๊ฒƒ์„ ๋ณด๊ณ  li์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ƒˆ๋กœ ๋ Œ๋”๋งํ•จ
  • ๊ทธ๋ž˜์„œ key๊ฐ€ ํ•„์š”ํ•จ

4. key

//๋ณ€๊ฒฝ์ „
<ul>
  <li key="1">first</li>
  <li key="2">second</li>
</ul>

//๋ณ€๊ฒฝํ›„
<ul>
  <li key="๊ทธ๋ž˜ํ•ด๋ผ">Firstํ• ๊ฑฐ์•ผ!</li> //์—ฌ๊ธฐ๋งŒ ์ถ”๊ฐ€๋˜์—ˆ์ง€๋งŒ ์ฒซ๋ฒˆ์งธ ์ˆœ์„œ
  <li key="1">first</li>
  <li key="2">second</li>
</ul>
  • key๋ฅผ ์ง€์ •ํ•ด์ฃผ๋ฉด, ์ˆœ์„œ๊ฐ€ ๋‹ค๋ฅด๋”๋ผ๋„ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ Œ๋”๋งํ•˜์ง€ ์•Š๊ณ ๋ณ€ํ™”๊ฐ€ ์ƒ๊ธด ๋ถ€๋ถ„๋งŒ ๋ Œ๋”๋ง
  • key๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฆฌ์•กํŠธ์—์„œ warning์„ ๋ณด๋‚ด๋Š” ์ด์œ  ์ค‘ ํ•˜๋‚˜

์ฐธ๊ณ 
https://dj-min43.medium.com/%EB%A6%AC%EC%95%A1%ED%8A%B8-virtual-dom-%EA%B0%80%EC%83%81%EB%8F%94-%EB%B2%84%EC%B6%94%EC%96%BC%EB%8F%94-%EC%9D%B4%EB%9E%80-359c28112048
https://velog.io/@mollog/React%EC%97%90%EC%84%9C%EC%9D%98-%EA%B0%80%EC%83%81%EB%8F%94-%EA%B0%9C%EB%85%90