React/Velopert's React

[React] Study # 16 | useMemo

yevdev 2021. 10. 8. 10:32

๐Ÿ’ก useMemo?

  • ์„ฑ๋Šฅ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•˜์—ฌ ์—ฐ์‚ฐ๋œ ๊ฐ’์„ useMemo ๋ผ๋Š” Hook ์„ ์‚ฌ์šฉํ•˜์—ฌ ์žฌ์‚ฌ์šฉํ•œ๋‹ค.
  • ์ฒซ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ : ์–ด๋–ป๊ฒŒ ์—ฐ์‚ฐํ• ์ง€ ์ •์˜ํ•˜๋Š” ํ•จ์ˆ˜
  • ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ : deps ๋ฐฐ์—ด , ์ด ๋ฐฐ์—ด ์•ˆ์— ๋„ฃ์€ ๋‚ด์šฉ์ด ๋ฐ”๋€Œ๋ฉด, ์šฐ๋ฆฌ๊ฐ€ ๋“ฑ๋กํ•œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๊ฐ’์„ ์—ฐ์‚ฐํ•ด์ฃผ๊ณ , ๋งŒ์•ฝ์— ๋‚ด์šฉ์ด ๋ฐ”๋€Œ์ง€ ์•Š์•˜๋‹ค๋ฉด ์ด์ „์— ์—ฐ์‚ฐํ•œ ๊ฐ’์„ ์žฌ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค.

 

countActiveUsers๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  active๊ฐ’์ด true์ธ ์‚ฌ์šฉ์ž์˜ ์ˆ˜๋ฅผ ์„ธ์–ด์„œ ํ™”๋ฉด์— ๋ Œ๋”๋ง ํ•ด๋ณด์ž!

 

App.js

import React,{useRef, useState} from 'react'; 
import UserList from './UserList';
import CreateUser from './CreateUser';

// ํ™œ์„ฑ ์‚ฌ์šฉ์ž ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜
function countActiveUsers(users){
  console.log('ํ™œ์„ฑ ์‚ฌ์šฉ์ž ์ˆ˜๋ฅผ ์„ธ๋Š” ์ค‘...');
  return users.filter(user => user.active).length;
}

function App() {

  const [inputs, setInputs] = useState({
    username: '', email: ''
  })


  const {username, email} = inputs;

  const onChange = e => {

    const {name, value} = e.target;

    setInputs({
      ...inputs,
      [name]: value
    })
  }


  const [users, setUsers] = useState(
    // active ์†์„ฑ ์ถ”๊ฐ€
    [
      {id: 1, username: 'John', email: 'john@example.com',
      active: true,
    },
      {id: 2, username: 'Car', email: 'car@example.com',
      active: false,
    },
      {id: 3, username: 'lee', email: 'lee@example.com',
      active: false,
    },
    ]
)
    

const nextId = useRef(4);

const onCreate = () => {

    const user = {
      id: nextId.current,
      username,
      email
    };

    setUsers(users.concat(user));

    setInputs({
      username: '',
      email: ''
    })
    nextId.current += 1;
};

const onRemove = id => {
  setUsers(users.filter(user => user.id !== id))
}

const onToggle = id => {
  setUsers(
    users.map(user => 
      user.id === id ? { ...user, active: !user.active} : user
    )
  )
}

// ํ•จ์ˆ˜ ํ˜ธ์ถœ
const count = countActiveUsers(users);

  return (   
    <>
      <CreateUser 
      username={username}
      email={email}
      onChange={onChange}
      onCreate={onCreate}
      />
      <UserList users={users} onRemove={onRemove} onToggle={onToggle}/>
      <div>ํ™œ์„ฑ ์‚ฌ์šฉ์ž ์ˆ˜ : {count} </div>
    </>
  );
}

export default App;

๐Ÿšซํ•˜์ง€๋งŒ ์ด๋•Œ, ๋ฐœ์ƒํ•˜๋Š” ์„ฑ๋Šฅ์  ๋ฌธ์ œ

  • ๋ฐ”๋กœ input์— ๊ฐ’์„ ์ž…๋ ฅํ•  ๋•Œ๋„ countActiveUsers ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ
  • ํ™œ์„ฑ ์‚ฌ์šฉ์ž ์ˆ˜๋ฅผ ์„ธ๋Š” ๊ฑด users์— ๋ณ€ํ™”๊ฐ€ ์žˆ์„ ๋•Œ๋งŒ ์„ธ์•ผํ•˜๋Š” ๊ฑด๋ฐ, input ๊ฐ’์ด ๋ฐ”๋€” ๋•Œ์—๋„ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง ๋˜๋ฏ€๋กœ ์ด๋ ‡๊ฒŒ ๋ถˆํ•„์š”ํ•œ ํ˜ธ์ถœ๋กœ ์ธํ•ด ์ž์›์ด ๋‚ญ๋น„๊ฐ€ ๋˜๊ณ  ์žˆ๋‹ค.

๐Ÿ’ก์ด๋•Œ, useMemo ๋ผ๋Š” Hook ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์„ฑ๋Šฅ์„ ์ตœ์ ํ™” ํ•  ์ˆ˜ ์žˆ๋‹ค!

 

App.js

import React,{useRef, useState, useMemo} from 'react'; // useMemo ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
import UserList from './UserList';
import CreateUser from './CreateUser';

// ํ™œ์„ฑ ์‚ฌ์šฉ์ž ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜
function countActiveUsers(users){
  console.log('ํ™œ์„ฑ ์‚ฌ์šฉ์ž ์ˆ˜๋ฅผ ์„ธ๋Š” ์ค‘...');
  return users.filter(user => user.active).length;
}

function App() {

  const [inputs, setInputs] = useState({
    username: '', email: ''
  })


  const {username, email} = inputs;

  const onChange = e => {

    const {name, value} = e.target;

    setInputs({
      ...inputs,
      [name]: value
    })
  }


  const [users, setUsers] = useState(
    // active ์†์„ฑ ์ถ”๊ฐ€
    [
      {id: 1, username: 'John', email: 'john@example.com',
      active: true,
    },
      {id: 2, username: 'Car', email: 'car@example.com',
      active: false,
    },
      {id: 3, username: 'lee', email: 'lee@example.com',
      active: false,
    },
    ]
)
    

const nextId = useRef(4);

const onCreate = () => {

    const user = {
      id: nextId.current,
      username,
      email
    };

    setUsers(users.concat(user));

    setInputs({
      username: '',
      email: ''
    })
    nextId.current += 1;
};

const onRemove = id => {
  setUsers(users.filter(user => user.id !== id))
}

const onToggle = id => {
  setUsers(
    users.map(user => 
      user.id === id ? { ...user, active: !user.active} : user
    )
  )
}

// useMemo ์‚ฌ์šฉ
const count = useMemo(() => countActiveUsers(users),[users]);

  return (   
    <>
      <CreateUser 
      username={username}
      email={email}
      onChange={onChange}
      onCreate={onCreate}
      />
      <UserList users={users} onRemove={onRemove} onToggle={onToggle}/>
      <div>ํ™œ์„ฑ ์‚ฌ์šฉ์ž ์ˆ˜ : {count} </div>
    </>
  );
}

export default App;

 

 


Reference

https://react.vlpt.us/basic/17-useMemo.html