From e4f7df4df186f87c5daab26bd58db3f9f4abf04c Mon Sep 17 00:00:00 2001 From: jayming66 Date: Mon, 19 Aug 2024 12:55:59 +0900 Subject: [PATCH] docs: React: mount vs render ? --- Aug/article/React:mount-vs-render?.md | 101 ++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 Aug/article/React:mount-vs-render?.md diff --git a/Aug/article/React:mount-vs-render?.md b/Aug/article/React:mount-vs-render?.md new file mode 100644 index 0000000..eabcfd7 --- /dev/null +++ b/Aug/article/React:mount-vs-render?.md @@ -0,0 +1,101 @@ +# [React: "mount" vs "render"?](https://reacttraining.com/blog/mount-vs-render) + +### ๐Ÿ—“๏ธ ๋ฒˆ์—ญ ๋‚ ์งœ: 2024.08.19 + +### ๐Ÿงš ๋ฒˆ์—ญํ•œ ํฌ๋ฃจ: ๋งˆ์Šคํ„ฐ์œ„(๋ช…์žฌ์œ„) + +--- + +## React: "mount" vs "render" ? + +Brad Westfall +Updated On Jan 31, 2020 +Keyword : **React** + +์ƒˆ๋กœ์šด ๊ฒƒ์„ ๋ฐฐ์šฐ๋Š” ๊ฒƒ์€ ์ข…์ข… ๋งŽ์€ ์ƒˆ๋กœ์šด ์šฉ์–ด์™€ ํ•จ๊ป˜ ์˜ต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด React์—์„œ "mounting"๊ณผ "rendering"์˜ ์ฐจ์ด์ ์€ ๋ฌด์—‡์ผ๊นŒ์š”? + +### tldr; + +- "Rendering"์€ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ˜ธ์ถœ๋˜๊ฑฐ๋‚˜ ํด๋ž˜์Šค ๊ธฐ๋ฐ˜์˜ render ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜์–ด DOM์„ ์ƒ์„ฑํ•˜๋Š” ์ผ๋ จ์˜ ๋ช…๋ น์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ชจ๋“  ์ˆœ๊ฐ„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. +- "Mounting"์€ React๊ฐ€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ฒ˜์Œ "๋ Œ๋”๋ง"ํ•˜๊ณ , ์ด๋Ÿฌํ•œ ๋ช…๋ น์„ ์‚ฌ์šฉํ•˜์—ฌ ์ดˆ๊ธฐ DOM์„ ์‹ค์ œ๋กœ ๊ตฌ์ถ•ํ•˜๋Š” ๊ณผ์ •์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. + +"์žฌ๋ Œ๋”๋ง(re-render)"์€ React๊ฐ€ ์ด๋ฏธ ๋งˆ์šดํŠธ๋œ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ƒˆ๋กœ์šด ๋ช…๋ น ์„ธํŠธ๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‹ค์‹œ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฐ€๋ฅด์น˜๋ฉด์„œ "๋งˆ์šดํŒ…(mounting)"๊ณผ "๋ Œ๋”๋ง(rendering)"์— ๋Œ€ํ•œ ์งˆ๋ฌธ์„ ํด๋ž˜์Šค ๊ธฐ๋ฐ˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฐ€๋ฅด์น  ๋•Œ๋ณด๋‹ค ๋” ์ž์ฃผ ๋ฐ›๊ธฐ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค. ์™œ ๊ทธ๋Ÿฐ์ง€ ์ดํ•ดํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋จผ์ € ํด๋ž˜์Šค ๊ธฐ๋ฐ˜ ์ปดํฌ๋„ŒํŠธ์—์„œ "๋งˆ์šดํŒ…"๊ณผ "๋ Œ๋”๋ง"์˜ ์ฐจ์ด๋ฅผ ์„ค๋ช…ํ•˜๊ณ , ๊ทธ ํ›„ ๋™์ผํ•œ ๊ฐœ๋…์„ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์™€ ๋น„๊ตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +React๊ฐ€ ์ฒ˜์Œ์œผ๋กœ ๋งŒ๋‚˜๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ `App`์ด๋ผ๊ณ  ๊ฐ€์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ์ปดํฌ๋„ŒํŠธ๋ฅผ `ReactDOM.render`์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. + +```tsx +class App extends React.Component { + state = { + showUser: false, + }; + + render() { + return ( +
+ {this.state.showUser && } + + +
+ ); + } +} + +ReactDOM.render(, document.getElementById('root')); +``` + +React๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ `App`์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์ดˆ๊ธฐ DOM์„ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์ฒซ ๋ฒˆ์งธ ๋ช…๋ น ์„ธํŠธ๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด `render` ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ํด๋ž˜์Šค ๊ธฐ๋ฐ˜ ์ปดํฌ๋„ŒํŠธ์—์„œ render ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ์ด๋ฅผ "๋ Œ๋”๋ง"์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ Œ๋”๋ง์—์„œ๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ "๋งˆ์šดํŒ…"๋œ๋‹ค๊ณ  ํ•˜๋ฉฐ, React๊ฐ€ DOM์— ๋ช…๋ น์„ ์ ์šฉํ•˜๊ณ  ์ด์ „์— ์กด์žฌํ•˜์ง€ ์•Š์•˜๋˜ DOM์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +์‚ฌ์šฉ์ž๊ฐ€ `Show User` ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค. ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค React๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ "์žฌ๋ Œ๋”๋ง"ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ƒํƒœ ๋ณ€๊ฒฝ์— ๋”ฐ๋ผ ์ƒˆ๋กœ์šด ๋ช…๋ น์„ ์–ป๊ธฐ ์œ„ํ•ด render ๋ฉ”์„œ๋“œ๋ฅผ ๋‹ค์‹œ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ปดํฌ๋„ŒํŠธ์˜ ๊ฒฝ์šฐ, ์ด ์žฌ๋ Œ๋”๋ง์—์„œ `showUser` ๊ฐ’์ด `true`๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ’์ด `true`๊ฐ€ ๋˜๋ฉด, ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฐ˜ํ™˜๋œ ๋ช…๋ น์€ React์—๊ฒŒ DOM ์—…๋ฐ์ดํŠธ๊ฐ€ ํ•„์š”ํ•จ์„ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ๋งˆ์šดํŒ… ๋‹จ๊ณ„๋Š” ์ฒซ ๋ฒˆ์งธ ๋ Œ๋”๋ง์—์„œ๋งŒ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ๋‘ ๋ฒˆ์งธ ๋ Œ๋”๋ง์€ ๋งˆ์šดํŒ…์ด ์•„๋‹ˆ๋ฉฐ, DOM์—์„œ ์–ด๋–ค ๋ถ€๋ถ„์ด ์—…๋ฐ์ดํŠธ๋˜์–ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ํŒŒ์•…ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. React๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ์ด์ „ ๋ช…๋ น๊ณผ ์ƒˆ๋กœ์šด ๋ช…๋ น์„ ๋น„๊ตํ•˜์—ฌ DOM์—์„œ ์‹ค์ œ๋กœ ์—…๋ฐ์ดํŠธ๊ฐ€ ํ•„์š”ํ•œ ๋ถ€๋ถ„์„ ํ™•์ธํ•˜๋Š”๋ฐ, ์ด๋ฅผ "[์กฐ์ •(reconciliation)](https://legacy.reactjs.org/docs/reconciliation.html)"์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. + +์ค‘์š”ํ•œ ์ ์€ ์ฒซ ๋ฒˆ์งธ "๋ Œ๋”๋ง"์ด React๊ฐ€ DOM์— ํ•ญ๋ชฉ์„ "๋งˆ์šดํŒ…"ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ด์–ด์ง€๋ฉฐ, ์žฌ๋ Œ๋”๋ง์€ DOM์„ ์—…๋ฐ์ดํŠธํ•˜์ง€๋งŒ ๋งˆ์šดํŒ…์€ ํ•œ ๋ฒˆ๋งŒ ๋ฐœ์ƒํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. + +์ด์ œ ์œ„์˜ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ์จ๋ด…์‹œ๋‹ค. ๋Œ€์‹  ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์™€ ํ›…๊ณผ ํ•จ๊ป˜ ์”๋‹ˆ๋‹ค: + +```tsx +function App() { + const [showUser, setShowUser] = useState(false); + + return ( +
+ {showUser && } + + +
+ ); +} +``` + +ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋„ ๋ Œ๋”์™€ ๋งˆ์šดํŠธ ๊ณผ์ •์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. React๊ฐ€ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ(๋ Œ๋”๋ง)๋˜๊ณ  ๋ฐ˜ํ™˜๋œ ๋ช…๋ น์ด ๋งˆ์šดํŠธ์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋Š” ์ „์ฒด ํ•จ์ˆ˜๊ฐ€ ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์˜ render ๋ฉ”์„œ๋“œ์™€ ์œ ์‚ฌํ•˜๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ์—์„œ "render"๋ผ๋Š” ๋‹จ์–ด๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ๋ณด์ง€ ์•Š๋”๋ผ๋„ ๋ง์ž…๋‹ˆ๋‹ค. + +ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ด์ œ ์ƒํƒœ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ์ปดํฌ๋„ŒํŠธ๋Š” ์ด์ „๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์žฌ๋ Œ๋”๋ง์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + +```tsx +// ๋ฆฌ์•กํŠธ๊ฐ€ ์šฐ๋ฆฌ์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ renderํ•˜๊ธฐ ์‹œ์ž‘ํ•˜๊ณ  ์ด๊ฒƒ์ด ์ฒซ๋ฒˆ์งธ render์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ด๊ฒƒ์ด DOM์— ์ปดํฌ๋„ŒํŠธ๋ฅผ "mounts'์ž…๋‹ˆ๋‹ค. +App(); + +// ์ƒํƒœ๊ฐ€ ๋ฐ”๋€Œ๊ณ  ๋ฆฌ์•กํŠธ๋Š” ์ด์ œ ์ƒˆ๋กœ์šด ์ƒํƒœ๊ฐ€ ๋†“์€ ์ปดํฌ๋„ŒํŠธ ์žฌ๋ Œ๋”๋ง์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. +App(); + +// ์ƒํƒœ๊ฐ€ ๋‹ค์‹œ ๋ฐ”๋€Œ๊ณ  ๋ฆฌ์•กํŠธ์˜ ์žฌ๋ Œ๋”๋ง์ด ๋‹ค์‹œ ์ผ์–ด๋‚ฉ๋‹ˆ๋‹ค. +App(); +``` + +์ดˆ๊ธฐ ๋ Œ๋”๋ง์€ ๋งˆ์šดํŒ…์„ ์œ ๋„ํ•˜๋ฉฐ, ์žฌ๋ Œ๋”๋ง์€ ๋‹จ์ˆœํžˆ ํ•จ์ˆ˜๋ฅผ ๋‹ค์‹œ ํ˜ธ์ถœํ•˜๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค. + +์ด์ œ ๋‘ ์ปดํฌ๋„ŒํŠธ, `User`์™€ `App`์„ ์˜ˆ๋กœ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. + +React๋Š” ์ฒ˜์Œ์— App์„ ๋ Œ๋”๋งํ•˜์ง€๋งŒ User๊ฐ€ ๋ฐ˜ํ™˜๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— User๋Š” React์—๊ฒŒ ์•Œ๋ ค์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ์‹œ์ ์—์„œ App์€ ๋งˆ์šดํŠธ๋˜์ง€๋งŒ User๋Š” ๋งˆ์šดํŠธ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +๊ทธ ํ›„ showUser๊ฐ€ true๋กœ ์„ค์ •๋˜๋ฉด `App`์ด ์žฌ๋ Œ๋”๋ง๋˜๊ณ , `User`๋ฅผ ํฌํ•จํ•œ ๋ช…๋ น์ด ๋ฐ˜ํ™˜๋˜์–ด `User`๊ฐ€ ์ฒ˜์Œ ๋ Œ๋”๋ง๋˜๊ณ  ๋งˆ์šดํŠธ๋ฉ๋‹ˆ๋‹ค. + +์‚ฌ์šฉ์ž๊ฐ€ `Hide User` ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋˜์–ด `App`์ด ๋‹ค์‹œ ์žฌ๋ Œ๋”๋ง๋ฉ๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ๋ช…๋ น๊ณผ ์ด์ „ ๋ Œ๋”๋ง์—์„œ ๋ฐ˜ํ™˜๋œ ๋ช…๋ น์ด ๋น„๊ต๋˜๊ณ , React๋Š” `User`๊ฐ€ ๋” ์ด์ƒ `App`์—์„œ ๋ฐ˜ํ™˜๋˜์ง€ ์•Š์Œ์„ ๊ฐ์ง€ํ•˜์—ฌ `User`๋ฅผ DOM์—์„œ "์–ธ๋งˆ์šดํŠธ"ํ•ฉ๋‹ˆ๋‹ค. + +์ปดํฌ๋„ŒํŠธ ์•„ํ‚คํ…์ฒ˜์—์„œ ๋ถ€๋ชจ ๋…ธ๋“œ๋Š” ์ž์‹ ๋…ธ๋“œ๋ฅผ ์ธ์‹ํ•˜์ง€๋งŒ ์ž์‹ ๋…ธ๋“œ๋Š” ๋ถ€๋ชจ ๋…ธ๋“œ๋ฅผ ์ธ์‹ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. React๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žฌ๋ Œ๋”๋ง๋  ๋•Œ๋งˆ๋‹ค ์ž์‹ ๋…ธ๋“œ๋„ ์žฌ๋ Œ๋”๋งํ•˜๋Š” ๊ฒƒ์ด ์ž์—ฐ์Šค๋Ÿฌ์šด ํŠน์„ฑ์ž…๋‹ˆ๋‹ค. + +๊ฐ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ž์ฒด์ ์ธ ๋งˆ์šดํŒ… ๋ฐ ๋ Œ๋”๋ง "์Šค์ผ€์ค„"์„ ๊ฐ€์ง€๋”๋ผ๋„, ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋Š” ๋ถ€๋ชจ๊ฐ€ ๋งˆ์šดํŠธ๋œ ๊ฒฝ์šฐ์—๋งŒ ๋ Œ๋”๋ง ๋ฐ ๋งˆ์šดํŠธ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์–ธ๋งˆ์šดํŠธ๋˜๋ฉด, ๊ทธ ์ž์‹๋“ค๋„ ์–ธ๋งˆ์šดํŠธ๋ฉ๋‹ˆ๋‹ค.