04. React useCallBack(), useMemo()

oh_bomΒ·2022λ…„ 11μ›” 7일
0

λ¦¬μ•‘νŠΈ(React)

λͺ©λ‘ 보기
5/9

useCallBack()

const memoizedCallback = useCallback(fn(ν•¨μˆ˜),deps(λ°°μ—΄));
:memoization 된 callback 리턴

memoization μ΄λž€?
:λ©”λͺ¨μ΄μ œμ΄μ…˜μ€ 컴퓨터 ν”„λ‘œκ·Έλž¨μ΄ λ™μΌν•œ 계산을 λ°˜λ³΅ν•΄μ•Ό ν•  λ•Œ, 
이전에 κ³„μ‚°ν•œ 값을 λ©”λͺ¨λ¦¬μ— μ €μž₯ν•¨μœΌλ‘œμ¨ λ™μΌν•œ κ³„μ‚°μ˜ 반볡 μˆ˜ν–‰μ„ μ œκ±°ν•˜μ—¬ ν”„λ‘œκ·Έλž¨ μ‹€ν–‰ 속도λ₯Ό λΉ λ₯΄κ²Œ ν•˜λŠ” 기술

첫번째 인자인 ν•¨μˆ˜λ₯Ό μ €μž₯ν›„, λ‘λ²ˆμ§Έ 인자인 μ˜μ‘΄μ„± λ°°μ—΄ κ°’ λ³€κ²½μ‹œ ν•¨μˆ˜λ₯Ό μƒˆλ‘œ 생성

useCallBack μ‚¬μš© μ „: μ»΄ν¬λ„ŒνŠΈ λ Œλ”λ§ λ§ˆλ‹€ μƒˆλ‘œμš΄ ν•¨μˆ˜κ°€ 생성
useCallBack μ‚¬μš© ν›„ : μ»΄ν¬λ„ŒνŠΈ λ Œλ”λ§μ΄ λ˜λ”λΌλ„ μ˜μ‘΄μ„± λ°°μ—΄ 값이 λ³€κ²½λ˜μ§€ μ•ŠλŠ”ν•œ κΈ°μ‘΄ ν•¨μˆ˜ μž¬μ‚¬μš©

μ˜ˆμ‹œμ½”λ“œ

  • μ˜μ‘΄μ„± 배열에 아무것도 μ—†λ‹€λ©΄, μ»΄ν¬λ„ŒνŠΈ 졜초 λ Œλ”λ§ μ‹œμ—λ§Œ ν•¨μˆ˜κ°€ μƒμ„±λ˜κ³  κ·Έ ν›„μ—λŠ” λ™μΌν•œ 참쑰값을 μ‚¬μš©ν•˜λŠ” ν•¨μˆ˜κ°€ 됨.
  • μ˜μ‘΄μ„± 배열이 μ½œλ°±μ— 인자둜 μ „λ‹¬λ˜μ§€λŠ” μ•Šμ§€λ§Œ, μ½œλ°±μ•ˆμ—μ„œ μ°Έμ‘°λ˜λŠ” λͺ¨λ“  값은 μ˜μ‘΄μ„± κ°’μ˜ 배열에 λ‚˜νƒ€λ‚˜μ•Ό ν•œλ‹€.(살짝 이해가 될듯말듯..? 더 κ³΅λΆ€ν•˜λ©΄μ„œ κΉ¨λ‹¬μ•„λ³΄μž,,)

μ°Έκ³ μžλ£ŒπŸš€:
https://www.daleseo.com/react-hooks-use-callback/
https://ko.reactjs.org/docs/hooks-reference.html#usecallback
https://devbirdfeet.tistory.com/134
λ”°λΌν•˜λ©° λ°°μš°λŠ” λ¦¬μ•‘νŠΈ κ°•μ˜μžλ£Œ

useMemo()

const memoizedValue = useMemo(()=>fn,deps);
:memoization 된 κ°’ 리턴

첫번재 인자인 fn은 결과값을 μƒμ„±ν•΄μ£ΌλŠ” ν•¨μˆ˜μ΄κ³ , depsλŠ” μž¬μ‚¬μš©μ˜ μ—¬λΆ€λ₯Ό κ²°μ •ν•˜λŠ” μ˜μ‘΄μ„±λ°°μ—΄.
μ˜μ‘΄μ„±λ°°μ—΄μ˜ λ³€κ²½μ‹œμ—λ§Œ λ©”λͺ¨μ΄μ œμ΄μ…˜λœ κ°’ λ‹€μ‹œ 계산.

μ˜ˆμ‹œμ½”λ“œ

const z=useMemo(()=>compute(x,y),[x,y]);

μ‹€μ œλ‘  생각보닀 많이 μ•ˆμ“΄λŒ€μš”..?

μ°Έκ³ μžλ£ŒπŸš€:
https://www.daleseo.com/react-hooks-use-memo/

profile
λͺ©ν‘œλŠ” κ°μžνƒˆμΆœ

0개의 λŒ“κΈ€