๋ฐ˜์‘ํ˜•

๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์˜ ๋ฆฌ๋ Œ๋”๋ง ์›์ธ์„ ํŒŒ์•…ํ•  ๋•Œ ๋ฆฌ์•กํŠธ ๊ฐœ๋ฐœ์ž ๋„๊ตฌ์˜ Profiler(ํ”„๋กœํŒŒ์ผ๋Ÿฌ) ํƒญ์„ ํ™œ์šฉํ•œ๋‹ค.

 

๋งŒ์•ฝ Profiler ํƒญ์—์„œ Hook 1 changed๋ฅผ ํ‘œ์‹œํ•œ๋‹ค๋ฉด, Hook 1์€ Components ํƒญ → ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์˜ hooks ํ•ญ๋ชฉ์—์„œ 1๋ฒˆ์งธ ๋‚ด์žฅ ํ›…์„(useState ๋“ฑ) ๊ฐ€๋ฆฌํ‚จ๋‹ค.

 

์ด๋•Œ ์ฃผ์˜ํ•  ์ ์€ Profiler์—์„œ ํ›…์„ ์นด์šดํŠธํ•  ๋•Œ useContext, ์ปค์Šคํ…€ ํ›…์€ ์ƒ๋žตํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋Œ€์‹  ์ปค์Šคํ…€ ํ›…์€ ๋‚ด๋ถ€์— ์žˆ๋Š” ๋ชจ๋“  ๋‚ด์žฅ ํ›…(useContext ์ œ์™ธ)์„ ์นด์šดํŠธ ํ•œ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด ๋‚ด์žฅ ํ›… 1๊ฐœ์™€ ์ปค์Šคํ…€ ํ›…์„ ์‚ฌ์šฉํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž. ์ปค์Šคํ…€ ํ›… ์•ˆ์—์„  ๋‚ด์žฅ ํ›… 2๊ฐœ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค. ๊ทธ๋Ÿผ ์ด ์ปดํฌ๋„ŒํŠธ์˜ ํ›… ์ธ๋ฑ์Šค๋Š” ์ปดํฌ๋„ŒํŠธ ๋‚ด์žฅ ํ›… 1, ์ปค์Šคํ…€ ํ›… ๋‚ด์žฅ ํ›… 2, 3์ด ๋œ๋‹ค. ์ปค์Šคํ…€ ํ›…์— ์žˆ๋Š” 2๋ฒˆ์งธ ๋‚ด์žฅ ํ›… ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋ฉด Profiler์—์„  Hook 3 changed๋ฅผ ํ‘œ์‹œํ•œ๋‹ค.

function useHooks(defaultValue = 1) {
  const [hookTwo, setHookTwo] = useState(defaultValue); // ๋‚ด์žฅ ํ›… Hook 2
  const [hookThree, setHookThree] = useState(defaultValue); // ๋‚ด์žฅ ํ›… Hook 3

  return [hookTwo, setHookTwo, hookThree, setHookThree];
}

function App() { 
  const [hookOne, setHookOne] = useState(1); // ๋‚ด์žฅ Hook 1
  const [hookTwo, setHookTwo, hookThree, setHookThree] = useHooks(); // ์ปค์Šคํ…€ ํ›…

  return (
    <div className="App">
      <button onClick={() => setHookOne((prev) => prev + 1)}>{`hookOne: ${hookOne}`}</button>
      <button onClick={() => setHookTwo((prev) => prev + 1)}>{`hookTwo: ${hookTwo}`}</button>
      <button onClick={() => setHookThree((prev) => prev + 1)}>{`hookThree: ${hookThree}`}</button>
    </div>
  );
}

 

hookThree ์ƒํƒœ๋Š” ์ปค์Šคํ…€ ํ›…์˜ ๋‘๋ฒˆ์งธ ๋‚ด์žฅ ํ›…(useState)์ด๋‹ค. ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ด์„œ ์ด ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ฉด ๋ฆฌ์•กํŠธ ๊ฐœ๋ฐœ์ž ๋„๊ตฌ Profiler ํƒญ์—์„œ Hook 3 changed๋ฅผ ํ‘œ์‹œํ•œ๋‹ค.

 

Hook 3์€ Components ํƒญ → App ์ปดํฌ๋„ŒํŠธ → Hooks(์ปค์Šคํ…€ ํ›…์˜ ๋‚ด์žฅ ํ›…)์˜ ๋‘๋ฒˆ์งธ ํ•ญ๋ชฉ์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ์ฆ‰, ๋ฆฌ์•กํŠธ ๊ฐœ๋ฐœ์ž ๋„๊ตฌ์—์„  ์ปค์Šคํ…€ ํ›… ์ž์ฒด๋Š” ์นด์šดํŠธ ํ•˜์ง€ ์•Š๊ณ , ์ปค์Šคํ…€ ํ›… ๋‚ด๋ถ€์— ์žˆ๋Š” ๋‚ด์žฅ ํ›…์„ ์นด์šดํŠธ ํ•œ๋‹ค.

 

๐Ÿ’ก ์•„๋ž˜ 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ปดํฌ๋„ŒํŠธ ๋ฆฌ๋ Œ๋”๋ง ์›์ธ์„ ์ถ”์  ํ•ด๋ณผ ์ˆ˜๋„ ์žˆ๋‹ค(์จ๋“œํŒŒํ‹ฐ๋Š” ์ถ”์  ํž˜๋“ฆ)

 

๐Ÿ’ก ๋ฆฌ๋ Œ๋”๋ง์ด ๋งŽ์œผ๋ฉด ๋ฆฌ์•กํŠธ Profiler๋ฅผ ํ†ตํ•œ ์ถ”์ ํ•˜๊ธฐ ๊นŒ๋‹ค๋กœ์›Œ์ง„๋‹ค. ์ด๋• ํฌ๋กฌ Performance(์„ฑ๋Šฅ) ํƒญ์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋งํฌ ์ฐธ๊ณ .

 


๊ธ€ ์ˆ˜์ •์‚ฌํ•ญ์€ ๋…ธ์…˜ ํŽ˜์ด์ง€์— ๊ฐ€์žฅ ๋น ๋ฅด๊ฒŒ ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค. ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”
๋ฐ˜์‘ํ˜•