[React-React Hooks] useMemo

๐Ÿ“„ useMemo

useMemo๋Š” ์„ฑ๋Šฅ ์ตœ์ ํ™” ๋‹จ๊ณ„์—์„œ ์—ฐ์‚ฐ๋œ ๊ฐ’์„ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

import { useMemo } from "react";

const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);
  • ์ฒซ ๋ฒˆ์งธ ์ธ์ž: ์ฝœ๋ฐฑ ํ•จ์ˆ˜
  • ๋‘ ๋ฒˆ์งธ ์ธ์ž: ์˜์กด์„ฑ ๋ฐฐ์—ด

๋‘ ๋ฒˆ์งธ ์ธ์ž์ธ ๋ฐฐ์—ด์˜ ์š”์†Œ ๊ฐ’์ด ์—…๋ฐ์ดํŠธ๋  ๋•Œ๋งŒ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ๋‹ค์‹œ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ ๋นˆ ๋ฐฐ์—ด์„ ๋„˜๊ฒจ์ฃผ๋ฉด ๋งจ ์ฒ˜์Œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋งˆ์šดํŠธ ๋˜์—ˆ์„ ๋•Œ๋งŒ ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๊ณ  ์ดํ›„์—๋Š” memoizaton๋œ ๊ฐ’์„ ๊บผ๋‚ด์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์›ํ•˜๋Š” ๊ฐ’์ด ๋ฐ”๋€Œ์ง€ ์•Š์œผ๋ฉด ๋ฆฌ๋ Œ๋”๋งํ•  ๋•Œ ์ด์ „์˜ ๊ฐ’์„ ์žฌ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“„ useMemo ์‚ฌ์šฉ๋ชฉ์ 

  • ์ด์ „์— ์—ฐ์‚ฐ๋œ ๊ฐ’์„ ์žฌ์‚ฌ์šฉํ•œ๋‹ค.
  • ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง๋œ ๊ฒฐ๊ณผ๋ฅผ ์žฌ์‚ฌ์šฉํ•œ๋‹ค.

๐Ÿ“„ ์ฃผ์˜ํ•  ์ 

useMemo๋Š” ๊ผญ ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋งŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๊ฐ’์„ ์žฌํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋”ฐ๋กœ ๋ฉ”๋ชจ๋ฆฌ์— ๊ฐ’์„ ์ €์žฅํ•ด ๋†“๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๋ถˆํ•„์š”ํ•œ ๊ฐ’์„ ์ €์žฅํ•˜๋ฉด ์„ฑ๋Šฅ์ด ์•ˆ์ข‹์•„์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ถœ์ฒ˜

Leave a comment