Suspense
๐Ÿš™

Suspense

Description
Published
2023-12-19
Tags

React์˜ ๋น„๋™๊ธฐ ์ž‘์—… ์ฒ˜๋ฆฌ์™€ Suspense์˜ ๋“ฑ์žฅ

React์—์„œ๋Š” 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ๋ Œ๋”๋ง ์‹œ ๋น„๋™๊ธฐ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • fetch on render: ์ปดํฌ๋„ŒํŠธ ๋ Œ๋”๋ง์„ ๋จผ์ € ์‹œ์ž‘ํ•˜๊ณ  useEffect๋‚˜ componentDidMount๋กœ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค.
  • fetch then render: useEffect๋‚˜ componentDidMount๋กœ ํ™”๋ฉด์„ ๊ทธ๋ฆฌ๋Š”๋ฐ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ์กฐํšŒํ•œ ํ›„ ๋ Œ๋”๋ง์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
  • render as you fetch: ๋น„๋™๊ธฐ ์ž‘์—…๊ณผ ๋ Œ๋”๋ง์„ ๋™์‹œ์— ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰์‹œ ์ดˆ๊ธฐ ์ƒํƒœ๋ฅผ ๋ Œ๋”๋ง(fallback rendering)ํ•˜๊ณ , ๋น„๋™๊ธฐ ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด ๋‹ค์‹œ ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค.
ย 
fetch on render๋‚˜ fetch then render ๋ฐฉ์‹์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์–ด๋ ค์›€์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • Race Condition
    • ๋น„๋™๊ธฐ ์ž‘์—…๋“ค์ด ๊ฐ€์ง€๋Š” ์ž์‹ ๋งŒ์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋กœ ์ธํ•ด Race Condition์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
    • fetching๊ณผ ๋ Œ๋”๋ง ์‚ฌ์ด์— ๋™๊ธฐํ™”๋ฅผ ํ†ตํ•ด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • Waterfall Problem
      • ๋™๊ธฐํ™” ์ฒ˜๋ฆฌ๋กœ ์ธํ•œ ๋™์‹œ์„ฑ์ด ๋ถˆ๊ฐ€๋Šฅํ•ด์ง€๊ณ  ํšจ์œจ์„ฑ์ด ๋–จ์–ด์ง‘๋‹ˆ๋‹ค.
    • High Coupling
      • ๋น„๋™๊ธฐ ์ž‘์—…๋“ค์„ ํ•œ ๊ณณ์—์„œ ๋™์‹œ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ปดํฌ๋„ŒํŠธ๋กœ ์ „๋‹ฌํ•˜๋ฉด ๋™์‹œ์„ฑ์„ ๋ณด์žฅํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ปดํฌ๋„ŒํŠธ๋“ค ๊ฐ„์˜ ์—ญํ• ๋ถ„๋‹ด์ด ๋ถˆ๋ช…ํ™•ํ•ด์ง€๊ณ  ๊ฒฐํ•ฉ๋„๊ฐ€ ๋†’์•„์ง‘๋‹ˆ๋‹ค.
      ย 
      ์œ„์™€ ๊ฐ™์€ ์–ด๋ ค์›€์œผ๋กœ ์ธํ•ด render as you fetch๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋Š” Suspense๊ฐ€ ๋“ฑ์žฅํ•˜๊ฒŒ ๋์Šต๋‹ˆ๋‹ค.
      ย 

      Suspense์˜ ๋™์ž‘ ๋ฐฉ์‹

      Suspense๋Š” useEffect๋‚˜ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋‚ด๋ถ€์—์„œ ํŽ˜์นญํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ฐ์ง€ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
      ย 
      Suspense๋ฅผ ๋™์ž‘์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ๋Š” children์— ์†ํ•œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋น„๋™๊ธฐ ์ž‘์—…์„ ์ง„ํ–‰ํ• ๋•Œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋กœ Promise๋ฅผ throwํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.
      ย 
      ๋˜ํ•œ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด Suspense ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
      • Suspense๋ฅผ ๋„์ž…ํ•œ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•œ ๋ฐ์ดํ„ฐ ํŽ˜์นญ
      • lazy๋ฅผ ์‚ฌ์šฉํ•œ ์ง€์—ฐ ๋กœ๋”ฉ ์ปดํฌ๋„ŒํŠธ ์ฝ”๋“œ
        ย 

        Suspense์˜ ์‚ฌ์šฉ๋ฒ•

        Suspense๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด children์ด ๋กœ๋”ฉ์„ ์™„๋ฃŒํ•  ๋•Œ๊นŒ์ง€ fallback์„ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
        fallback์€ ๋กœ๋”ฉ์ด ์™„๋ฃŒ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์— ๋ณด์—ฌ์ฃผ๋Š” ๋Œ€์ฒด UI๋กœ ์ฃผ๋กœ ๋กœ๋”ฉ ์Šคํ”ผ๋„ˆ๋‚˜ ์Šค์ผˆ๋ ˆํ†ค์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
        Suspense๋Š” children์ด ์ผ์‹œ ์ค‘๋‹จ๋˜๋ฉด ์ž๋™์œผ๋กœ fallback์œผ๋กœ ์ „ํ™˜๋˜๊ณ , ๋ฐ์ดํ„ฐ๊ฐ€ ์ค€๋น„๋˜๋ฉด ๋‹ค์‹œ children์œผ๋กœ ์ „ํ™˜๋ฉ๋‹ˆ๋‹ค.
        ย 
        ๊ธฐ๋ณธ์ ์œผ๋กœ Suspense ๋‚ด๋ถ€์˜ ์ „์ฒด ํŠธ๋ฆฌ๋Š” ๋‹จ์ผ ๋‹จ์œ„๋กœ ์ทจ๊ธ‰๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ ์ปดํฌ๋„ŒํŠธ ์ค‘ ํ•˜๋‚˜๋งŒ ๋ฐ์ดํ„ฐ ๋Œ€๊ธฐ๋ฅผ ์œ„ํ•ด ์ผ์‹œ ์ค‘๋‹จํ•˜๋”๋ผ๋„ ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ•จ๊ป˜ ๋กœ๋”ฉ ํ‘œ์‹œ๊ธฐ๋กœ ๋Œ€์ฒด๋ฉ๋‹ˆ๋‹ค
        ย 
        ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ผ์‹œ ์ค‘๋‹จ๋˜๋ฉด ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์ƒ์œ„ Suspense ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํด๋ฐฑ์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์—ฌ๋Ÿฌ Suspense ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ค‘์ฒฉํ•˜์—ฌ ๋กœ๋”ฉ ์‹œํ€€์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
        ย 

        ๊ฐ‘์ž‘์Šค๋Ÿฌ์šด ๊ฒฐ๋ก 

        Suspense ์‚ฌ์šฉ๋ฒ•์„ ์ตํžˆ๊ณ  ์บ์‹ฑ์„ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๊ธฐ ์–ด๋ ค์šฐ๋‹ˆ react-query๋ฅผ ์“ฐ์ž!
        useDeferredValue ํ›…, useTransition ํ›…์€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค.
        ย 

        ์ฐธ๊ณ