header-img
Info :
728x90

 

์ฐธ๊ณ  ๊ธ€ ์›๋ฌธ

 

Making Sense of React Server Components

This year, the React team unveiled something they've been quietly researching for years: an official way to run React components exclusively on the server. This is a significant paradigm shift, and it's caused a whole lot of confusion in the React communit

www.joshwcomeau.com

 

(์ด๋ฒˆ ๊ธ€์˜ ๊ฒฝ์šฐ ํ•ด๋‹น ๊ธ€์— ๋ง๋ถ™ํ˜€ ์„ค๋ช…์ด ๋ถ€์กฑํ•˜๋‹ค๊ณ  ๋Š๊ปด์ง€๋Š” ๊ฒƒ๋“ค์€ ๋‹ค์ˆ˜ ์„œ์นญํ•˜์—ฌ ์ž‘์„ฑํ•˜์—ฌ.. ์ฐธ๊ณ  ๊ธ€์ด๋ผ๊ณ  ํ•˜๊ธฐ์—๋„ ๋จธ์“ฑํ•˜๋‹ค.)

โ€‹

React18์—์„œ ๋„์ž…๋œ ๊ฐœ๋…์ธ, ๋ฆฌ์•กํŠธ ์„œ๋ฒ„ ์ปดํฌ๋„ŒํŠธ(React Server Component) ์— ๋Œ€ํ•˜์—ฌ ํฌ์ŠคํŒ… ํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค.

๋ฆฌ์•กํŠธ ์„œ๋ฒ„ ์ปดํฌ๋„ŒํŠธ๋Š” ๋‹จ์–ด ๊ทธ๋Œ€๋กœ React ์—์„œ ์ œ๊ณตํ•˜๋Š”, ์„œ๋ฒ„์—์„œ ๋™์ž‘ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

(ํด๋ผ์ด์–ธํŠธ ์ปดํฌ๋„ŒํŠธ๋„ ์žˆ๋ƒ? ๋ผ๊ณ  ๋ฌผ์œผ๋ฉด.. ํ•ด๋‹น ๊ธฐ์ˆ ์ด ์ ์šฉ๋˜์ง€ ์•Š์€ ์ด์ „์˜ ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ๋“ค์ด ๋ฐ”๋กœ ํด๋ผ์ด์–ธํŠธ ์ปดํฌ๋„ŒํŠธ์ด๋‹ค.)

โ€‹

๋“ค์–ด๊ฐ€๊ธฐ์— ์•ž์„œ.. ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์„ ํ–‰๋˜๋Š” ๊ฐœ๋…์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

React18์—์„œ ๋„์ž…๋œ ๊ฐœ๋…์ด ๋ฆฌ์•กํŠธ ์„œ๋ฒ„ ์ปดํฌ๋„ŒํŠธ ๋ผ๋ฉด, ๊ทธ ์ด์ „์—๋Š” ์–ด๋–ค ์‹์œผ๋กœ ๋ Œ๋”๋ง์„ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ์—ˆ์„๊นŒ?

โ€‹

~ React17 ๊นŒ์ง€์˜ ๋ Œ๋”๋ง

 

React17 ๊นŒ์ง€์˜ ๋ Œ๋”๋ง์€ CSR๊ณผ SSR๋กœ ์„ค๋ช…๋œ๋‹ค.

 

โ‘  CSR (Client Side Rendering)

React18 ์ด์ „์˜ ๋ฆฌ์•กํŠธ ์ƒ์˜ ์ปดํฌ๋„ŒํŠธ์˜ ๋ชจ๋“  ๋ Œ๋”๋ง์€ ํด๋ผ์ด์–ธํŠธ(๋ธŒ๋ผ์šฐ์ €)์—์„œ ์ด๋ฃจ์–ด์ง.

 

๋กœ์ง

- ์„œ๋ฒ„๋Š” React๋กœ ์ž‘์„ฑํ•œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ ๋ธŒ๋ผ์šฐ์ €์— ์‘๋‹ตํ•˜๊ณ ,

- ๋ธŒ๋ผ์šฐ์ €๋Š” ์‘๋‹ต๋ฐ›์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ํ•ด์„ํ•˜์—ฌ HTMLํŒŒ์ผ๋กœ ๋ Œ๋”๋งํ•˜๊ณ , ์ธํ„ฐ๋ ‰์…˜์ด ํ•„์š”ํ•œ ์ฝ”๋“œ๋“ค(event listener ๋“ฑ)์„ ์žฅ์ฐฉ

โ€‹

โ‘ก SSR (Server Side Rendering)

๊ธฐ๋ณธ react.js ์˜ ๊ฒฝ์šฐ๋Š” CSR. Next,js ์˜ ์ผ€์ด์Šค๊ฐ€ ์„œ๋ฒ„ ์‚ฌ์ด๋“œ ๋ Œ๋”๋ง์— ํ•ด๋‹น๋˜๋Š” ๋“ฏ. (Next.js๊ฐ€ ๋‚˜์˜ค๋ฉด์„œ SSR์„ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋จ.)

๋กœ์ง

-Next.js ๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„ ๋‹จ์—์„œ 1) ํŽ˜์ด์ง€๋ณ„๋กœ ์ฝ”๋“œ๋ฅผ ์ชผ๊ฐ  ํ›„(Code Split) 2) HTML์„ ๋ Œ๋”๋งํ•œ ๋‹ค์Œ, 3) ๋ธŒ๋ผ์šฐ์ €์— ์‘๋‹ตํ•˜๋ฉด.

- ๋ธŒ๋ผ์šฐ์ €๋Š” ์ „๋‹ฌ ๋ฐ›์€ 1) HTML์„ ๋…ธ์ถœํ•˜๊ณ , 2) ์ถ”๊ฐ€๋กœ ์ „๋‹ฌ ๋ฐ›์€ JS ์ฝ”๋“œ๋“ค(event listener)์„ ๊ฒฐํ•ฉํ•˜๋Š” Hydration ๊ณผ์ •์„ ๊ฑฐ์นจ.

โ€ป Hydration: ๋ผˆ๋Œ€ ๋ฟ์ธ HTML์— JS์ฝ”๋“œ๋ฅผ ๊ฒฐํ•ฉ์‹œ์ผœ ์ธํ„ฐ๋ž™์…˜(click event ์ฒ˜๋ฆฌ ๊ฐ™์€ ๊ฒƒ)์„ ์žฅ์ฐฉํ•˜๋Š” ๊ณผ์ •..

โ€‹

Next.js ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ๋„ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ธŒ๋ผ์šฐ์ €์—์„œ ๋ Œ๋”๋ง์ด ๋˜๋Š” CSR์ด ๊ธฐ๋ณธ.

SSR์„ ์ž‘์„ฑํ•˜๋ ค๋ฉดใ…ก, ๊ธฐํƒ€ ์ถ”๊ฐ€์ ์ธ ์ฝ”๋“œ ์ž‘์„ฑ์ด๋‚˜ ์„ค์ •์„ ํ•ด์ฃผ์–ด์•ผ ํ–ˆ์Œ...

โ€‹

React18 ์ดํ›„์˜ ๋ Œ๋”๋ง

๋‘ ๊ฐ€์ง€ ํ˜•์‹์œผ๋กœ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค. (in next13)

โ€‹

โ€‹

โ‘  Server Component

์„œ๋ฒ„์—์„œ HTML์„ ๋ Œ๋”๋ง ํ•ด์„œ ๋ธŒ๋ผ์šฐ์ €์— ์‘๋‹ตํ•˜๋Š” ๋ฐฉ์‹

ํ•˜์ด๋“œ๋ ˆ์ด์…˜(Hydration) ๊ณผ์ •์ด ์—†์Œ

โ€‹

โ‘ก Client Component

์„œ๋ฒ„์—์„œ ๊ธฐ๋ณธ์ ์ธ HTML์„ ๋ Œ๋”๋งํ•˜์—ฌ ๋ธŒ๋ผ์šฐ์ €์— ์‘๋‹ตํ•˜๋Š” ๋ฐฉ์‹

Hydration(JS Interactivity)๋งŒ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ด๋ค„์ง.

โ€‹

๊ธฐ๋ณธ์ ์ธ HTML ์ž์ฒด๋Š” ๋ชจ๋‘ ์„œ๋ฒ„์—์„œ ๋ Œ๋”๋ง ๋จ..

๋‹จ, Client Component์˜ ๊ฒฝ์šฐ์—๋งŒ ๋ธŒ๋ผ์šฐ์ €์—์„œ ํ•˜์ด๋“œ๋ ˆ์ด์…˜ ๊ณผ์ •์ด ๋ฐœ์ƒ, !!

โ€‹

๊ทธ๋Ÿฌ๋ฏ€๋กœ..

Event Listener(ํด๋ฆญ ์ด๋ฒคํŠธ ๋“ฑ), Browser API(Web Storage ๋“ฑ), React Hooks(useState, useEffect ๋“ฑ) ๋“ฑ์€ ๋ธŒ๋ผ์šฐ์ €์—์„œ๋งŒ ์ž‘๋™ํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ ๋‹จ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๊ธฐ ๋•Œ๋ฌธ์—..

Hydration ๊ณผ์ •์„ ๊ฑฐ์น˜์ง€ ์•Š๋Š” Server Component์—์„œ๋Š” ์ •์˜๊ฐ€ ๋  ์ˆ˜ ์—†์Œ. !!!

 

+) ํ˜„์žฌ ๋ฆฌ์•กํŠธ ์„œ๋ฒ„ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•ด๋ณผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ next.js 13๋ฒ„์ „์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋ฟ์ž„.

โ€‹

React Server Component์˜ ์žฅ์ 

  • ์„œ๋ฒ„ ์ „์šฉ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์ตœ์ดˆ์˜ "๊ณต์‹์ ์ธ" ๋ฐฉ๋ฒ•
  • ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ์„œ๋ฒ„ ์ „์šฉ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์ƒ๊ฒผ๋‹ค
  • ์„ฑ๋Šฅ ํ–ฅ์ƒ.
  • ๋” ์ด์ƒ ๊ธฐ๋Šฅ๊ณผ ๋ฒˆ๋“ค ํฌ๊ธฐ์˜ trade-off ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š์•„๋„ ๋จ

โ€‹

์ •๋ฆฌํ•ด๋ณด๋ฉด

 


1. ์ƒˆ๋กœ์šด ํŒจ๋Ÿฌ๋‹ค์ž„์—์„œ ์šฐ๋ฆฌ๊ฐ€ ์ž˜ ์•Œ๊ณ  ์žˆ๋Š” "ํ‘œ์ค€" ๋ฆฌ์•กํŠธ ํ”„๋ ˆ์ž„์›Œํฌ๋Š” "ํด๋ผ์ด์–ธํŠธ ์ปดํฌ๋„ŒํŠธ" ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋ฆฌ๋ธŒ๋žœ๋”ฉ ๋จ.
โ€‹
2. ์ƒˆ๋กœ์šด ํŒจ๋Ÿฌ๋‹ค์ž„์—์„œ ์„œ๋ฒ„์—๋งŒ ๋ Œ๋”๋ง์ด ๋˜๋Š” ์ƒˆ๋กœ์šด ์œ ํ˜•์˜ ์ปดํฌ๋„ŒํŠธ์ธ '์„œ๋ฒ„ ์ปดํฌ๋„ŒํŠธ'๊ฐ€ ์ถ”๊ฐ€. ํ•ด๋‹น ์ฝ”๋“œ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฒˆ๋“ค์— ํฌํ•จ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ hydrate ํ•˜๊ฑฐ๋‚˜ rerendering์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

ํ•ด๋‹น ๋‚ด์šฉ์„ ๋ณด์•„... ๋ฌด์กฐ๊ฑด์ ์œผ๋กœ ์ƒˆ๋กœ๋‚˜์˜จ ๊ฒƒ์ด ์ข‹๋‹ค! ๋ผ๋Š” ๊ฐœ๋…๋ณด๋‹ค๋Š”

์„œ๋ฒ„ ์ปดํฌ๋„ŒํŠธ์™€ ํด๋ผ์ด์–ธํŠธ์˜ ๋กœ์ง์ ์ธ ํŠน์ง•์„ ์ž˜ ์•Œ๊ณ .. ์ ์žฌ์ ์†Œ์— ์‚ฌ์šฉํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„๋ณด์ธ๋‹ค. (์‹ฌ์ง€์–ด ์ƒˆ๋กœ ๋‚˜์˜จ ๊ฐœ๋… ๋˜ํ•œ ์•„๋‹ˆ๋ฉฐ, ๊ธฐ๋ณธ ์‚ฌ์šฉ์ž๋“ค์ด ์šฐํšŒํ•˜์—ฌ ์“ฐ๋˜ ๊ธฐ๋Šฅ์„ ๋ฆฌ์•กํŠธ๊ฐ€ ๊ณต์‹์ ์ธ ๋ฐฉ๋ฒ•์„ ์ œ์•ˆํ•ด์ค€ ๋Šฌ์•™์Šค์ž„.)

โ€‹

๊ทธ๋ ‡๋‹ค๋ฉด..

โ€‹

 

Server Component์™€ Client Component๋Š” ๊ฐ๊ฐ ์–ธ์ œ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์„๊นŒ?

โ€‹

Client Component

- interactivity ๋ฐ Event Listener ์‚ฌ์šฉ์ด ํ•„์š”ํ•  ๋•Œ

- React Hook (useState, useEffect ๋“ฑ) ์‚ฌ์šฉ์ด ํ•„์š”ํ•  ๋•Œ

- Browser ๋‹จ์˜ API (web storage ๋“ฑ) ์‚ฌ์šฉ์ด ํ•„์š”ํ•  ๋•Œ

โ€‹

Server Component

-์œ„์˜ Client Component ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€ํ”ผํ•œ ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๊ฒฝ์šฐ ...

โ€‹

์ฐธ๊ณ ๋กœ ๊ณต์‹ ๋ฌธ์„œ์—์„œ๋Š” ์•„๋ž˜์˜ case์—์„œ ์„œ๋ฒ„ ํด๋ผ์ด์–ธํŠธ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์„ ๊ฐ•์ถ”ํ•˜๊ณ  ์žˆ๋‹ค.

1. fetch Data
2. Access Backend Resources(directly)
3. Keep sensitive information on the server (access tokens, API keys, etc)
4. Keep large dependencies on the server / Reduce client-side Javascript

โ€‹

์ „๋ฐ˜์ ์œผ๋กœ ์„œ๋ฒ„ ํ†ต์‹ ๊ณผ ๊ด€๋ จ์žˆ๋Š” ๋‚ด์šฉ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ๊ฐ•์ถ”ํ•˜๋Š” ๋“ฏ

 

 

 

ํ™”๋ฉด์„ ์˜ˆ์‹œ๋กœ ๋“ค์–ด๋ณด์ž๋ฉด

์—ฌ๊ธฐ์„œ

๋‹จ์ˆœํžˆ html๋กœ ์ด๋ฃจ์–ด์ ธ ์œ ์ € ์ธํ„ฐ๋ ‰์…˜์ด ์—†์„ ๊ฒƒ์œผ๋กœ ์ถ”์ •๋˜๋Š” sidebar, Main, Navbar ์ปดํฌ๋„ŒํŠธ๋Š” ์„œ๋ฒ„ ์ปดํฌ๋„ŒํŠธ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ์ด๊ณ 

Search๋‚˜ Button ๊ฐ™์€ ์œ ์ €์ธํ„ฐ๋ ‰์…˜์ด ์ผ์–ด๋‚˜ react hook์œผ๋กœ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜์—ฌ์•ผ ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋“ค์€ ํด๋ผ์ด์–ธํŠธ ์ปดํฌ๋„ŒํŠธ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„ํ…Œ๋‹ค.

โ€ป ์„œ๋ฒ„ ์ปดํฌ๋„ŒํŠธ๋Š” ํด๋ผ์ด์–ธํŠธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํฌํ•จ ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Œ. ์—ญํ–‰์€ ๋ถˆ๊ฐ€.

 

 

 

728x90
๋”๋ณด๊ธฐ
FRONTEND/React