๊ด€๋ฆฌ ๋ฉ”๋‰ด

Daehyunii's Dev-blog

์ปดํฌ๋„ŒํŠธ ์Šคํƒ€์ผ๋ง, useMemo, useCallback, Custom Hook ๋ณธ๋ฌธ

๐Ÿ“š Language & CS knowledge/React

์ปดํฌ๋„ŒํŠธ ์Šคํƒ€์ผ๋ง, useMemo, useCallback, Custom Hook

Daehyunii 2022. 12. 19. 00:12
์ปดํฌ๋„ŒํŠธ ์Šคํƒ€์ผ๋ง

1. ์Šคํƒ€์ผ์‹œํŠธ ์ ์šฉ

  ์™ธ๋ถ€ CSSํŒŒ์ผ์„ importํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

2. Inline style ์ ์šฉ

  ๋™์ ์œผ๋กœ ์Šคํƒ€์ผ์„ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒฝ์šฐ์— ์š”์†Œ์˜ ์†์„ฑ์œผ๋กœ style์„ ์ฃผ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

3. CSS in JS ์ ์šฉ

  ์—ฌ๋Ÿฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ๋Š”๋ฐ ๊ฐ•์˜์—์„œ๋Š” emotion์ด๋ผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

yarn add @emotion/react @emotion/styled
 

@emotion/styled

styled API for emotion. Latest version: 11.10.6, last published: a month ago. Start using @emotion/styled in your project by running `npm i @emotion/styled`. There are 7665 other projects in the npm registry using @emotion/styled.

www.npmjs.com

 

useMemo

 

  ์ปดํฌ๋„ŒํŠธ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์ƒํ™ฉ์—์„œ ๋ฆฌ๋ Œ๋”๋ง ๋œ๋‹ค.

  • ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๋Š” ์ž์‹ ์˜ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ
  • ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ๋ฐ›๋Š” prop์ด ๋ณ€๊ฒฝ๋  ๋•Œ
  • ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ

  ๋งŒ์•ฝ ์—ฐ์‚ฐ ์†๋„๊ฐ€ ๋Š๋ฆฐ ์ปดํฌ๋„ŒํŠธ๋ผ๋ฉด ๋ Œ๋”๋ง ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆด ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•ด useMemo๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค.

๋‘๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฐฐ์—ด์„ ๋„˜๊ฒจ์ฃผ๋Š”๋ฐ, ๋ฐฐ์—ด ์•ˆ์˜ n์ด ๋ณ€๊ฒฝ ๋  ๋•Œ๋งŒ sum()ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚ค๊ธฐ ์œ„ํ•ด useMemo ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค๋ฅธ ์ƒํƒœ ๊ฐ’์˜ ๋ณ€๊ฒฝ๊ณผ ๊ด€๊ณ„์—†์ด ๋…๋ฆฝ์ ์œผ๋กœ ์‹คํ–‰ํ•˜๋„๋ก ๋งŒ๋“ค์–ด ์ฃผ์—ˆ๋‹ค.

const result = useMemo(() => sum(n), [n])

 

React.memo

 

  ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋Š” ๋ณ€๊ฒฝ๋˜์—ˆ์ง€๋งŒ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์•˜์„ ๋•Œ ๋ฆฌ๋ Œ๋”๋งํ•˜์ง€ ์•Š๋„๋ก ๋„์™€์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค. ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฆฌ๋ Œ๋”๋ง๋˜๋Š” ํ•จ์ˆ˜๋ฅผ React.memo๋กœ ๊ฐ์‹ธ์ฃผ๋ฉด ๋œ๋‹ค.

 

useCallback

 

  ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง๋  ๋•Œ ํ•จ์ˆ˜๊ฐ€ ๋‹ค์‹œ ์„ ์–ธ๋˜๋Š” ๊ฒƒ์„ ๋ง‰๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” hook์ด๋‹ค. checkbox ํ•˜๋‚˜๋ฅผ ํด๋ฆญํ–ˆ๋Š”๋ฐ Checkbox ์ปดํฌ๋„ŒํŠธ์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ํ•จ์ˆ˜๋“ค์ด ์ „๋ถ€ ์„ ์–ธ๋˜๋Š” ๋ฌธ์ œ๋ฅผ useCallback์„ ์‚ฌ์šฉํ•ด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

...
const foodChange = useCallback((e) => setFoodOn(e.target.checked), [])
const clothesChange = useCallback((e) => setClothesOn(e.target.checked), [])
const shelterChange = useCallback((e) => setShelterOn(e.target.checked), [])

return (
    <div>
        <Checkbox ... onChange={foodChange} />
        <Checkbox ... onChange={clothesChange} />
        <Checkbox ... onChange={shelterChange} />
    </div>
)

 

Custom Hook

 

  ์‚ฌ์šฉ์ž ์ •์˜ hook์€ ๊ธฐ์กด hook์„ ์กฐํ•ฉํ•ด์„œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ์˜ ์ค‘๋ณต์„ ์ค„์ผ ์ˆ˜ ์žˆ๊ณ  ์ปดํฌ๋„ŒํŠธ๋„ ๋” ๊น”๋”ํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

import { useCallback, useState } from 'react';

const useToggle = (initialState = false) => {
  const [state, setState] = useState(initialState);
  const toggle = useCallback(() => setState((state) => !state), []);

  return [state, toggle];
};

export default useToggle;
import useToggle from './useToggle';

function App() {
  const [on, toggle] = useToggle();

  return (
    <div>
      <button onClick={toggle}>{on ? 'true' : 'false'}</button>
    </div>
  );
}

export default App;