본문 바로가기
React

React useCallback 사용하기

by memory-log 2020. 9. 21.

useCallback

const memoizedCallback = useCallback(() => {
  doSomething(a, b)
}, [a, b])
  • 메모이제이션된 함수를 재사용

useCallback example

import React, { useCallback } from "react"
export default function MyParent({ term }) {
  const handleClick = useCallback(
    item => {
      console.log("You clicked ", item)
    },
    [term]
  )

  return <MyBigList term={term} handleClick={handleClick} />
}
// Recreate increment on every change of delta!
const increment = useCallback(() => setC(c => c + delta), [delta])
const increment = useCallback(() => {
  setCount(count + 1)
}, [count])

const decrement = useCallback(() => {
  setCount(count - 1)
}, [count])

const incrementOtherCounter = useCallback(() => {
  setOtherCounter(otherCounter + 1)
}, [otherCounter])

메모이제이션(memoization)

컴퓨터 프로그램이 동일한 계산을 반복해야 할 때, 이전에 계산한 값을 메모리에 저장함으로써 동일한 계산의 반복 수행을 제거하여 프로그램 실행 속도를 빠르게 하는 기술

'React' 카테고리의 다른 글

react covid-19 tracker web site  (0) 2020.12.20
리액트 함수형 컴포넌트 리팩토링하기  (0) 2020.09.30
리액트 컴포넌트 절대경로 import  (0) 2020.09.30
React Lazyload  (0) 2020.09.21
React useMemo 사용하기  (0) 2020.09.21

댓글