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]);
• 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)