[React] useMemo (React-hooks)

JIOOยท2022๋…„ 4์›” 19์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
7/18
post-thumbnail

๐Ÿ‘ฉโ€๐ŸŽ“ useMemo๋ž€ ๋ญ˜๊นŒ?

์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žฌ ๋ Œ๋”๋ง ๋  ๋•Œ ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ์—†์œผ๋ฉด ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ฒ˜์Œ ๋ Œ๋”๋ง ๊ฐ’์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ


๐Ÿ‘ฉโ€๐ŸŽ“ ์ผ๋‹จ useMemo๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„  ๋ฆฌ์•กํŠธ์˜ ๋ Œ๋”๋ง ๊ตฌ์กฐ๋ฅผ ์•Œ์•„์•ผํ•œ๋‹ค

ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ ๊ธฐ์ค€์œผ๋กœ ๋ Œ๋”๋ง ๊ตฌ์กฐ๋ฅผ ์•Œ์•„๋ณด์ž


๐Ÿ‘ฉโ€๐ŸŽ“ ๋ฆฌ์•กํŠธ์˜ ๋ Œ๋”๋ง ๊ตฌ์กฐ

์ปดํฌ๋„ŒํŠธ ๋žœ๋”๋ง โ†’ ๊ฐ’ ๋ณ€๊ฒฝ โ†’ ๋ณ€๊ฒฝ๊ฐ’์„ ๋“ค๊ณ  ์ปดํฌ๋„ŒํŠธ๋ฅผ ์žฌ ํ˜ธ์ถœ
โ†’ ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€ ๋ฐ์ดํ„ฐ&๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™” โ†’ ๊ฐ’ ์—…๋ฐ์ดํŠธ ์™„๋ฃŒ

์ด๋•Œ ๊ฐ’์„ ์—…๋ฐ์ดํŠธ ํ•  ๋•Œ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š” ๊ฐ’ ๊นŒ์ง€ ๋‹ค์‹œ ์ดˆ๊ธฐํ™” ํ•˜๋Š” ๊ฒƒ์ด ๋น„ ํšจ์œจ์  ์ด๋ผ๋Š” ํŒ๋‹จ์ด ๋“ฌ

๐Ÿ’ก ๊ทธ๋ž˜์„œ ๋‚˜์˜จ๊ฒŒ useMemo ์ด๋‹ค


๐Ÿ‘ฉโ€๐ŸŽ“ useMemo์˜ ๊ธฐ๋ณธ์ฝ”๋“œ

const ๋ฉ”๋ชจ = useMemo(()=>{
   return ๊ฐ’
},[component]) //useEffect์™€ ๊ฐ™์€ ๋…ผ๋ฆฌ

์„ค์ •๋œ ์ปดํฌ๋„ŒํŠธ์˜ ๊ฐ’์ด ์—…๋ฐ์ดํŠธ ๋  ๋•Œ ๋งˆ๋‹ค ๋ฉ”๋ชจ์˜ useMemo๊ฐ€ ์‹คํ–‰

useMemo ์‹ค์ „์—์„œ ์จ๋ณด๊ธฐ

๐Ÿ‘ฉโ€๐ŸŽ“ ์˜ˆ์ œ)

const [number,setNumber] = useState(0);
const [isboolean,setIsboolean] = usestate(true)

const showBoolean = {
  return isboolean ? "์„ฑ๊ณต" : "์‹คํŒจ"
}

useEffect(()=>{
 console.log("์ฝ˜์†”์ฐฝ ๋œน๋‹ˆ๋‹ค")
},[showBoolean])

<input type="number" value={number} onChange={(e)=> setNumber(e.target.value)}/>
<p>ํ˜„์žฌ์ˆซ์ž๋Š”? :  {number}</p>
<button onClick={() => setIsboolean(!isboolean)}>boolean๋ณ€๊ฒฝ</button>
<div>{showBoolean}</div>

๐Ÿ‘ฉโ€๐ŸŽ“ ์ด๋ ‡๊ฒŒ ์žˆ๋‹ค๋Š” ๊ฐ€์ •, number์˜ ๊ฐ’์„ ์ฆ๊ฐ€ ์‹œํ‚ฌ๋ ค๊ณ  ํ•œ๋‹ค.

number์˜ state๋ฅผ ์ฆ๊ฐ€ ์‹œ์ผฐ์„ ๋•Œ, ๊ฑด๋“ค์ด์ง€ ์•Š์€ showBoolean(์ด)๊ฐ€
์˜์กด๋˜์–ด ์žˆ๋Š” useEffect๊ฐ€ ์‹คํ–‰์ด ๋˜๋Š” ์ƒํƒœ์ด๋‹ค, ์ด๊ฑธ ํ•ด๊ฒฐํ•ด์•ผํ•˜๋Š” ๋ฌธ์ œ


๐Ÿ‘ฉโ€๐ŸŽ“ ๋ฌธ์ œ๋ถ„์„

  1. number์˜ ๊ฐ’์„ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด state๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด์„œ ์žฌ ๋ Œ๋”๋ง์ด ๋ฐœ์ƒ ํ•˜๋Š”๋ฐ ์žฌ ๋ Œ๋”๋ง ์‹œ
    ์ปดํฌ๋„ŒํŠธ์•ˆ์— ์„ ์–ธ๋œ [number,isboolean,showBlooean] ์ด๋Ÿฐ ๋ฐ์ดํ„ฐ๋“ค์ด ๋‹ค ์ดˆ๊ธฐํ™” ๋˜๊ธฐ
    ๋•Œ๋ฌธ์— ๋น„ ํšจ์œจ์  ์žฌ ๋ Œ๋”๋ง์ด ๋ฐœ์ƒ

  2. ์ด๋•Œ ๊ฐ์ฒด๋Š” ์ดˆ๊ธฐํ™” ๋ ๋•Œ ๋ฐ”๋กœ showBoolean์— ์ €์žฅ ๋˜์ง€์•Š๊ณ  ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ์—
    (์บ์‹ฑ-showBoolean) ์ €์žฅ ํ–ˆ๋‹ค๊ฐ€ ๋ณ€์ˆ˜์— ๋‹ด๊ฒจ showBoolean.name ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉ ํ•˜๋Š”๋ฐ
    ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žฌ ๋ Œ๋”๋ง ์‹œ ์ด์ „ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ ๋˜์—ˆ๋˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐธ์กฐํ•ด์„œ
    ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ (์บ์‹ฑ2-showBoolean)๋กœ ์ƒˆ๋กœ ๋ฎ์–ด์“ฐ๊ณ  ๋ณ€์ˆ˜์— ์ €์žฅ ํ•จ

  3. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— useEffect๋กœ showBoolean์„ ์˜์กด์„ฑ ์„ค์ • ํ–ˆ์„ ๊ฒฝ์šฐ showBoolean์˜ ๊ฐ’์ด ๋ฐ”๋€Œ์ง€ ์•Š์•„๋„ ์บ์‹ฑ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐ”๋€Œ๊ธฐ ๋•Œ๋ฌธ์— useEffect๊ฐ€ ์‹คํ–‰์ด ๋˜๋Š” ๊ฒƒ์ด๋‹ค

๐Ÿ‘ฉโ€๐ŸŽ“ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ• ๊นŒ?

๐Ÿ’ก useEffect์— ์˜์กด๋˜์–ด ์žˆ๋Š” showBoolean์„ useMemo๋กœ ๋ฉ”๋ชจ๋ผ์ด์ฆˆ ์‹œํ‚ค๋ฉด ๋œ๋‹ค

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

export default function App() {
  const [numbers, setNumber] = useState(0);
  const [isboolean, setIsboolean] = useState(true);
  
  const showBoolean = useMemo(() => {
    return isboolean ? "์„ฑ๊ณต" : "์‹คํŒจ";
  }, [isboolean]);
  
  useEffect(() => {
    console.log("์ฝ˜์†”์ฐฝ ๋œน๋‹ˆ๋‹ค");
  }, [showBoolean]);
  
  //์ด๋ ‡๊ฒŒ showBoolean์„ useMemo๋กœ ๋งŒ๋“ค์–ด ๋ฒ„๋ฆฌ๋ฉด input์—์„œ onChange ํ•  ๋•Œ state๊ฐ€ ๋ฐ”๋€Œ์–ด๋„ 
  console์ด ์ฐํžˆ์ง€ ์•Š๋Š”๋‹ค. ์™œ๋ƒํ•˜๋ฉด showBoolean์— ๊ฑธ๋ ค์žˆ๋Š” isboolean์ด ์•ˆ๋ฐ”๋€Œ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  
  return (
    <div>
      <input
        type="number"
        value={numbers}
        onChange={(e) => setNumber(e.target.value)}
      />
      <p>ํ˜„์žฌ์ˆซ์ž๋Š”? : {numbers}</p>
      <button onClick={() => setIsboolean(!isboolean)}>boolean๋ณ€๊ฒฝ</button>
      <div>{showBoolean}</div>
    </div>
  );
}

์ด๋ ‡๊ฒŒ ๋ฐ”๊ฟ”์ฃผ๋ฉด ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žฌ ๋ Œ๋”๋ง ๋˜๋„ useMemo๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ’์ด ๋ฐ”๋€Œ์ง€ ์•Š์•„
useEffect๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.

๐Ÿ’ก๊ฒฐ๋ก  : useMemo๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋• ๊ทธ ์‹์„ useMemo๋กœ ๊ฐ์‹ธ๋ฉด ๋˜๊ณ , ์ฝคํฌ๋„ŒํŠธ๊ฐ€ ์žฌ๋žœ๋”๋ง ๋  ๋•Œ ์˜์กด์„ฑ ๋ฐฐ์—ด์— ๊ฑธ๋ ค์žˆ๋Š” state๊ฐ€ ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค๋ฉด useMemo๋Š” ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค

usecallback๊ณผ์˜ ์ฐจ์ด

useMemo๋Š” return ์œผ๋กœ ๊ฐ’์„ ๋ฐ˜ํ™˜ ํ•˜์ง€๋งŒ usecallback์€ ํ•จ์ˆ˜์ž์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์˜ˆ์‹œ)

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

export default function App() {
  const [ex, setEx] = useState(0);
  const [why, setWhy] = useState(0);

  // useCallback ์ด () => {console.log(why)} ๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  const useCallbackReturn = useCallback(() => {
    console.log(why);
  }, [ex]);

  // useCallback ์ด ๋‹ด๊ฒจ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰
  useCallbackReturn();

  return (
    <>
      <button onClick={() => setEx((curr) => curr + 1)}>X</button>
      <button onClick={() => setWhy((curr2) => curr2 + 1)}>Y</button>
    </>
  );
}

ex๊ฐ€ ๋ณ€๊ฒฝ ๋์„ ๋•Œ useCallbackReturn์ด ์‹คํ–‰๋˜์–ด console์— why๊ฐ€ ์ฐํžŒ๋‹ค๋Š” ๊ฐ€์ •

๊ทธ ๋œป์€ ๋‘๋ฒˆ์งธ ๋ฒ„ํŠผ setWhy๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ๋ฒ„ํŠผ์„ ์•„๋ฌด๋ฆฌ ๋ˆŒ๋Ÿฌ๋„
usecallbackReturn ํ•จ์ˆ˜์— ๋‹ด๊ธด console.log(why) ์˜ ๊ฐ’์€ ์ดˆ๊ธฐ๊ฐ’์—์„œ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค (0)

ํ•˜์ง€๋งŒ setEx๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋Š” ์ฆ‰์‹œ ์—…๋ฐ์ดํŠธ ๋œ why์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

profile
ํ”„๋ก ํŠธ์—”๋“œ๊ฐ€ ์ข‹์€ ์›น์Ÿ์ด

0๊ฐœ์˜ ๋Œ“๊ธ€