DEV Community

Cover image for What is useCallback?
Narasimma Radhakrishnan
Narasimma Radhakrishnan

Posted on

What is useCallback?

useCallback is a React Hook that lets you cache (memoize) a function definition between re-renders. Instead of creating a brand-new function on every render, React returns the exact same function instance from memory unless one of its dependencies changes.

import { useCallback } from 'react';

const cachedFn = useCallback(() => {
  // Your function logic here
}, [dependencies]);

Enter fullscreen mode Exit fullscreen mode

• fn (First Argument): The function you want to cache.
• dependencies (Second Argument): An array of reactive values (props, state, or variables declared inside the component) that the function relies on. If any value in this array changes, React recreates the function

Top comments (0)