Menu

What is useCallback?
📰

What is useCallback?

DEV Community: frontend·Narasimma Radhakrishnan·14 days ago
#68P367yy
#devtofrontend#devto#dev#function#react
Reading 0:00
15s threshold
Cover image for What is useCallback?

Narasimma Radhakrishnan

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