ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

web/react hook

useMemo

dev_been94 2024. 12. 14. 15:33
728x90
๋ฐ˜์‘ํ˜•

useMemo

 

๐ŸŽฏ useMemo๋ž€ ๋ฌด์—‡์ผ๊นŒ?

**useMemo**๋Š” ๊ฐ’์„ ๊ณ„์‚ฐํ•  ๋•Œ ๋ถˆํ•„์š”ํ•œ ๋ฐ˜๋ณต ๊ณ„์‚ฐ์„ ๋ง‰์•„์ฃผ๋Š” ๋„๊ตฌ์˜ˆ์š”.
์‰ฝ๊ฒŒ ๋งํ•ด, **"ํ•„์š”ํ•  ๋•Œ๋งŒ ๊ณ„์‚ฐํ•˜๊ณ , ๊ฐ’์ด ๋ณ€ํ•˜์ง€ ์•Š์œผ๋ฉด ์ด์ „ ๊ฒฐ๊ณผ๋ฅผ ์žฌ์‚ฌ์šฉ"**ํ•ด์š”.

 

๐Ÿงฎ ๊ณ„์‚ฐ ์ด์•ผ๊ธฐ๋กœ ์ดํ•ดํ•˜๊ธฐ

์—ฌ๋Ÿฌ๋ถ„์ด ์ˆ˜ํ•™ ์ˆ™์ œ๋ฅผ ํ•œ๋‹ค๊ณ  ์ƒ์ƒํ•ด๋ณด์„ธ์š”.
์–ด๋–ค ์ˆ˜ํ•™ ๋ฌธ์ œ์˜ ๋‹ต์„ ํ•œ ๋ฒˆ ๊ณ„์‚ฐํ–ˆ๋Š”๋ฐ, ์นœ๊ตฌ๊ฐ€ ๋˜ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ๋ฌผ์–ด๋ณด๋ฉด ๋‹ค์‹œ ๊ณ„์‚ฐํ•  ํ•„์š”๊ฐ€ ์—†๊ฒ ์ฃ ?

  • ๋‹ต์„ ๊ธฐ์–ตํ•ด๋’€๋‹ค๊ฐ€ ์นœ๊ตฌ์—๊ฒŒ ๋ฐ”๋กœ ์•Œ๋ ค์ฃผ๋ฉด ๋ผ์š”.

React์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์˜ˆ์š”.
๋ณต์žกํ•œ ๊ณ„์‚ฐ์ด ๋งค๋ฒˆ ์‹คํ–‰๋˜๋ฉด ์„ฑ๋Šฅ์ด ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ, ๊ฐ’์ด ๋ฐ”๋€Œ์ง€ ์•Š๋Š” ํ•œ ๊ณ„์‚ฐ์„ ๊ฑด๋„ˆ๋›ฐ๊ณ  ๊ฒฐ๊ณผ๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๋Š” ๊ฑฐ์˜ˆ์š”.
์ด ์—ญํ• ์„ ํ•˜๋Š” ๊ฒŒ ๋ฐ”๋กœ **useMemo**์ž…๋‹ˆ๋‹ค! ๐Ÿš€

 

๐Ÿ› ๏ธ useMemo ๊ธฐ๋ณธ ์˜ˆ์ œ

์•„๋ž˜๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ์˜ˆ์š”.
์ž…๋ ฅ๊ฐ’์— ๋”ฐ๋ผ ๋ณต์žกํ•œ ๊ณ„์‚ฐ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณผ๊ฒŒ์š”.

import React, { useState, useMemo } from "react";

function ExpensiveCalculation({ num }) {
  console.log("๋ณต์žกํ•œ ๊ณ„์‚ฐ ์‹คํ–‰ ์ค‘... ๐Ÿ”„");
  let result = 0;
  for (let i = 0; i < 1000000000; i++) {
    result += num; // ํฐ ์ˆซ์ž๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ๋”ํ•ด์š” (์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๋Š” ์ž‘์—…)
  }
  return result;
}

function App() {
  const [count, setCount] = useState(0);
  const [input, setInput] = useState(1);

  // useMemo๋ฅผ ์‚ฌ์šฉํ•ด ๋ณต์žกํ•œ ๊ณ„์‚ฐ์„ ์ตœ์ ํ™”
  const calculatedValue = useMemo(() => ExpensiveCalculation({ num: input }), [input]);

  return (
    <div>
      <h1>useMemo ์˜ˆ์ œ</h1>
      <p>๋ณต์žกํ•œ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ: {calculatedValue}</p>
      <button onClick={() => setCount(count + 1)}>์นด์šดํ„ฐ ์ฆ๊ฐ€: {count}</button>
      <input
        type="number"
        value={input}
        onChange={(e) => setInput(Number(e.target.value))}
      />
    </div>
  );
}

export default App;

 

๐Ÿ” ์ฝ”๋“œ์—์„œ ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚ ๊นŒ์š”?

  1. ExpensiveCalculation ํ•จ์ˆ˜
    • ์ž…๋ ฅ๊ฐ’์— ๋”ฐ๋ผ ๋งค์šฐ ๋ณต์žกํ•˜๊ณ  ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ๊ณ„์‚ฐ์„ ํ•ฉ๋‹ˆ๋‹ค.
  2. useMemo๋กœ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ ์ €์žฅ
    • useMemo๋Š” input ๊ฐ’์ด ๋ฐ”๋€” ๋•Œ๋งŒ ExpensiveCalculation์„ ์‹คํ–‰ํ•ด์š”.
    • input ๊ฐ’์ด ๊ทธ๋Œ€๋กœ๋ผ๋ฉด ์ด์ „์— ๊ณ„์‚ฐ๋œ ๊ฒฐ๊ณผ๋ฅผ ์žฌ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  3. ์นด์šดํ„ฐ ๋ฒ„ํŠผ ํด๋ฆญ
    • count ์ƒํƒœ๊ฐ€ ๋ฐ”๋€Œ์–ด๋„ input ๊ฐ’์ด ๋ฐ”๋€Œ์ง€ ์•Š์œผ๋ฉด ๊ณ„์‚ฐ์„ ๋‹ค์‹œ ์‹คํ–‰ํ•˜์ง€ ์•Š์•„์š”.

โณ useMemo๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ

๋งŒ์•ฝ useMemo๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด?

  • count ๋ฒ„ํŠผ์„ ํด๋ฆญํ•  ๋•Œ๋งˆ๋‹ค ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋ณต์žกํ•œ ๊ณ„์‚ฐ์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
  • ์ž…๋ ฅ๊ฐ’ input์ด ๋ฐ”๋€Œ์ง€ ์•Š์•˜๋Š”๋ฐ๋„ ๋งค๋ฒˆ ๊ณ„์‚ฐ์„ ์ƒˆ๋กœ ํ•˜๋‹ˆ๊นŒ ์„ฑ๋Šฅ์ด ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ์–ด์š”.

 

๐Ÿš€ useMemo์˜ ์žฅ์ 

  1. ๋ถˆํ•„์š”ํ•œ ๊ณ„์‚ฐ์„ ํ”ผํ•  ์ˆ˜ ์žˆ์–ด์š”.
    • ๊ณ„์‚ฐ์— ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ์ž‘์—…์ผ ๋•Œ ์„ฑ๋Šฅ ์ตœ์ ํ™”์— ํšจ๊ณผ์ ์ด์—์š”.
  2. ๊ฐ’์ด ๋ฐ”๋€” ๋•Œ๋งŒ ๋‹ค์‹œ ๊ณ„์‚ฐ
    • ์˜์กด์„ฑ ๋ฐฐ์—ด(์œ„ ์˜ˆ์ œ์˜ [input])์— ์žˆ๋Š” ๊ฐ’์ด ๋ฐ”๋€Œ์ง€ ์•Š์œผ๋ฉด ๊ธฐ์กด ๊ฒฐ๊ณผ๋ฅผ ์žฌ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  3. React ์ปดํฌ๋„ŒํŠธ์˜ ์„ฑ๋Šฅ ํ–ฅ์ƒ
    • ๊ณ„์‚ฐ์„ ์ตœ์†Œํ™”ํ•ด์„œ ํ™”๋ฉด ๋ Œ๋”๋ง์ด ๋” ๋นจ๋ผ์ ธ์š”.

 

๐Ÿงฉ useMemo๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ฃผ์˜ํ•  ์ 

  1. ๋ชจ๋“  ๊ณ„์‚ฐ์— ์‚ฌ์šฉํ•  ํ•„์š”๋Š” ์—†์–ด์š”.
    • useMemo๋Š” ๋ณต์žกํ•œ ๊ณ„์‚ฐ์„ ์ตœ์ ํ™”ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์•„์š”.
    • ๊ฐ„๋‹จํ•œ ๊ณ„์‚ฐ์—๋Š” ์˜คํžˆ๋ ค ๋ถˆํ•„์š”ํ•œ ์ฝ”๋“œ๊ฐ€ ๋  ์ˆ˜ ์žˆ์–ด์š”.
  2. ์˜์กด์„ฑ ๋ฐฐ์—ด์„ ์ •ํ™•ํ•˜๊ฒŒ ์„ค์ •ํ•ด์•ผ ํ•ด์š”.
    • [input]์ฒ˜๋Ÿผ ์–ด๋–ค ๊ฐ’์ด ๋ฐ”๋€” ๋•Œ ๋‹ค์‹œ ๊ณ„์‚ฐํ• ์ง€ ์ •ํ™•ํžˆ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

๐ŸŒŸ ํ•œ ์ค„ ์š”์•ฝ

useMemo๋Š” ๋ถˆํ•„์š”ํ•œ ๋ฐ˜๋ณต ๊ณ„์‚ฐ์„ ๋ง‰๊ณ , ๊ฐ’์ด ๋ณ€ํ•  ๋•Œ๋งŒ ๊ณ„์‚ฐ์„ ์‹คํ–‰ํ•ด ์„ฑ๋Šฅ์„ ์ตœ์ ํ™”ํ•ด์ฃผ๋Š” ํ›…์ด์—์š”.

728x90
๋ฐ˜์‘ํ˜•

'web > react hook' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

useReducer  (0) 2024.12.14
useOptimistic  (0) 2024.12.14
useLayoutEffect  (0) 2024.12.14
useInsertionEffect  (0) 2024.12.14
useImperativeHandle  (0) 2024.12.14
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ
๋ฐ˜์‘ํ˜•
250x250