React useCallback Hook

The useCallback Hook in React provides a memoized callback function. Think of memoization as a way to cache a value to avoid recalculating it. This helps isolate resource-intensive functions so they don’t run on every render.

Tutorials dojo strip

The useCallback Hook only re-executes when one of its dependencies changes, which can boost performance.

The useCallback and useMemo Hooks are similar. The key difference is that useMemo returns a memoized value, while useCallback returns a memoized function.

Problem

A common reason to use useCallback is to prevent a component from re-rendering unless its props change.

In this example, you might assume the TaskList component won’t re-render unless the tasks change.

Example

index.js

JavaScript

TaskList.js

Run this and click the count increment button. You’ll notice the TaskList component re-renders even when the tasks don’t change.

Why doesn’t this work? Even with memo, the TaskList component re-renders because the tasks state and the addTask function remain unchanged when the count is incremented. This happens due to “referential equality”. Every time a component re-renders, its functions get recreated, meaning the addTask function changes.

JavaScript

Solution

To fix this, use the useCallback Hook to prevent the function from being recreated unless necessary.

Use useCallback to stop the TaskList component from re-rendering needlessly

Example

index.js

JavaScript

TaskList.js

Now the TaskList component will only re-render when the tasks prop changes.

JavaScript
Tutorials dojo strip