๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

React

[ React ] Suspense๋ž€ ๋ฌด์—‡์ผ๊นŒ?

 

React์˜ Suspense๋Š” React 18์—์„œ ์ •์‹์œผ๋กœ ์ง€์›๋œ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ์ง€์›ํ•˜๋Š” React์˜ ๋‚ด์žฅ ์ปดํฌ๋„ŒํŠธ๋กœ, ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์ง„ํ–‰๋˜๋Š” ๋™์•ˆ, ์‚ฌ์šฉ์ž์—๊ฒŒ ๋กœ๋”ฉ์— ๋Œ€ํ•œ ์ƒํƒœ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

 

 

 

Suspense๋Š” Suspense์˜ ๊ฒฝ๊ณ„๋ฅผ ์„ค์ •ํ•˜๊ณ , ๊ฒฝ๊ณ„ ๋‚ด์—์„œ ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋กœ๋“œ๋˜๋Š” ์ปดํฌ๋„ŒํŠธ๋‚˜ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋กœ๋”ฉ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค. ์ด๋Ÿฐ ๋กœ๋”ฉ ์ƒํƒœ๋Š” fallbackํ”„๋กœํผํ‹ฐ๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉ์ž์—๊ฒŒ ํ‘œ์‹œ๋œ๋‹ค. Suspense์˜ ์ž์‹ ์ปดํฌ๋„ŒํŠธ(children)๊ฐ€ ์‹ค์ œ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ์ดํ›„์— ๋ Œ๋”๋ง ๋˜๋Š” ์ปดํฌ๋„ŒํŠธ์ด๋‹ค.

 

 

<Suspense fallback={<Loading />}>
  <SomeComponent />
</Suspense>

 

 

์œ„์™€ ๊ฐ™์ด <Suspense>{children}</Suspense>๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ, React Suspense๋Š” ์ฃผ๋กœ React ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋น„๋™๊ธฐ ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ์„ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” React.lazy์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋œ๋‹ค.(React.lazy๋Š” ๋™์ ์œผ๋กœ ๊ฐ€์ ธ์˜ค๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉ๋˜์–ด, ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ•„์š”๋กœ ํ•˜๋Š” ์‹œ์ ์—์„œ๋งŒ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋‹ค.)

 

React.lazy๋Š” Suspense ์ปดํฌ๋„ŒํŠธ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ๋”์šฑ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, Suspense ์ปดํฌ๋„ŒํŠธ๋Š” React.lazy๋กœ ๋กœ๋“œํ•˜๋Š” ๋™์•ˆ ์ผ์‹œ์ ์œผ๋กœ ๋‹ค๋ฅธ ๋‚ด์šฉ์„ ๋ Œ๋”๋งํ•  ์ˆ˜ ์žˆ๊ฒŒ๋œ๋‹ค.

 

 

 

<Suspense fallback={<Loading />}>
  <Details artistId={artist.id} />
</Suspense>

function Details({ artistId }) {
  return (
    <>
      <Biography artistId={artistId} />
      <Panel>
        <Albums artistId={artistId} />
      </Panel>
    </>
  );
}

์œ„์™€ ๊ฐ™์ด ์ง์ ‘์ ์ธ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์•„๋‹ˆ๋”๋ผ๋„ ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค. (์ด๋™ ๊ฐ€๋Šฅ)

 

 

 

 

 

 

 

 

 


 

๐ŸŒธ ์ถœ์ฒ˜ ๐ŸŒธ

 

Suspense - React

React18