Agentic hosting is here. Connect to our MCP now ->

Blog

Learning how to tame React's useCallback hook

On its own, useCallback doesn't prevent a single re-render.

ยทby Matthew Sobieray

React is one of the most popular JavaScript libraries for building user interfaces, and Hooks, introduced in React 16.8, are a big part of why. Hooks let you use state and other React features in function components, which are now the standard way to write React code.

In this article, we'll dig into one specific Hook, useCallback. It's one of the most misunderstood Hooks, because it only helps in specific situations, and on its own, it doesn't make anything faster. You'll learn what it does, when it actually prevents re-renders, and why the React Compiler means you often don't need it anymore. All the examples were tested with React 19.

What is memoization?

Memoization is an optimization technique where you store the result of a computation, and reuse it the next time you need it with the same inputs, instead of computing it again. It's a form of caching.

React offers three tools for memoization:

  • useMemo caches the result of a calculation between renders.
  • useCallback caches a function definition between renders.
  • memo wraps a component so that it skips re-rendering when its props haven't changed.

useCallback vs useMemo

The key difference is that useMemo caches a value, while useCallback caches a function. Both take a dependency array, and return the same cached value or function on every render until one of the dependencies changes:

import { useCallback, useMemo } from 'react';
 
function SortedList({ values }) {
  // Caches the sorted array until `values` changes
  const sortedValues = useMemo(() => values.toSorted((a, b) => a - b), [values]);
 
  // Caches the function itself until `values` changes
  const logValues = useCallback(() => console.log(values), [values]);
 
  // ...
}

Like all Hooks, useMemo and useCallback must be called at the top level of a component or a custom Hook.

In fact, useCallback(fn, deps) is equivalent to useMemo(() => fn, deps): it's a shortcut for memoizing a function. Note that useCallback doesn't call the function or cache its result. It only gives you back the same function object, so you can pass it around without it looking like a new function.

Rendering and React

Why would you want the same function object? It has to do with how React renders your components.

When a component's state changes, React re-renders that component and, by default, all of its child components. Re-rendering means calling the component function again, which recreates every function declared inside it.

In JavaScript, two functions are only equal if they're the same object, even if they contain the same code:

// These variables contain the same code, but they're not equal
const hello = () => console.log('Hello Matt');
const hello2 = () => console.log('Hello Matt');
 
hello === hello2; // false
hello === hello; // true

So, on every render, React sees the functions declared in your component as new functions. Most of the time, that's completely fine: creating a function is cheap, and re-rendering simple components is fast.

It matters in two situations:

  1. You pass the function as a prop to a child component wrapped in memo. memo skips re-rendering the child when its props are unchanged, but a new function is always a changed prop, so the child re-renders anyway, unless you keep the function the same with useCallback.
  2. The function is a dependency of another Hook, such as useEffect or useMemo. A new function on every render would make the effect run, or the memo recalculate, on every render.

Performance benefits of useCallback

When it's used in the right place, useCallback helps your app skip work it doesn't need to do.

For example, say a dashboard displays a large data table or a chart that's slow to render, and the parent component also has a search field. Every keystroke in the search field re-renders the parent. If the chart is wrapped in memo and receives its event handlers through useCallback, React can skip re-rendering it while you type, and the page stays responsive.

But on its own, useCallback doesn't prevent a single re-render. It only keeps a function the same, and that matters only if something compares it, like memo or a Hook's dependency array.

Drawbacks of useCallback

Memoization isn't free, so consider these drawbacks before you add useCallback, and useMemo, everywhere:

  • Extra work on every render: React still creates the function you pass to useCallback on every render, and also compares the dependencies. If nothing uses the cached function in a way that matters, that's work for no benefit.
  • Memory: React keeps the cached function, and everything it references, in memory between renders.
  • Code complexity: Wrapping functions in Hooks makes code longer and harder to read, and you have to keep the dependency arrays correct. A missing dependency means your function uses stale values.

Use useCallback when you can point to the memo component or the Hook dependency that it's for, and when you've measured, or have good reason to expect, a real performance problem.

useCallback example

Here's a simple app with a Counter component that has two pieces of state and renders two Button components, each updating one of them. The Button component takes two props, handleClick and name, and logs to the console each time it renders:

import { useState } from 'react';
 
const Button = ({ handleClick, name }) => {
  console.log(`${name} rendered`);
  return <button onClick={handleClick}>{name}</button>;
};
 
const Counter = () => {
  console.log('counter rendered');
  const [countOne, setCountOne] = useState(0);
  const [countTwo, setCountTwo] = useState(0);
 
  return (
    <>
      {countOne} {countTwo}
      <Button handleClick={() => setCountOne(countOne + 1)} name="button1" />
      <Button handleClick={() => setCountTwo(countTwo + 1)} name="button2" />
    </>
  );
};
 
export default Counter;

When you click either button, the counter and both buttons re-render. Here's the console output when you click button1:

counter rendered
button1 rendered
button2 rendered

If you're running your app in development with Strict Mode, React renders components twice to help you find bugs, so you'll see every message twice. That doesn't happen in production.

useCallback alone doesn't help

You might think that wrapping the handlers in useCallback stops button2 from re-rendering when you click button1. Let's try it:

const incrementOne = useCallback(() => setCountOne(countOne + 1), [countOne]);
const incrementTwo = useCallback(() => setCountTwo(countTwo + 1), [countTwo]);

Pass incrementOne and incrementTwo to the buttons, click button1, and the output is exactly the same: both buttons re-render. When a parent re-renders, React re-renders all its children by default, whether their props changed or not. useCallback kept incrementTwo the same, but nothing checked.

Combine useCallback with memo

To skip re-rendering a component when its props haven't changed, wrap it in memo. Together with useCallback, that gives the result we want:

import { memo, useCallback, useState } from 'react';
 
const Button = memo(function Button({ handleClick, name }) {
  console.log(`${name} rendered`);
  return <button onClick={handleClick}>{name}</button>;
});
 
const Counter = () => {
  console.log('counter rendered');
  const [countOne, setCountOne] = useState(0);
  const [countTwo, setCountTwo] = useState(0);
 
  const incrementOne = useCallback(() => setCountOne(countOne + 1), [countOne]);
  const incrementTwo = useCallback(() => setCountTwo(countTwo + 1), [countTwo]);
 
  return (
    <>
      {countOne} {countTwo}
      <Button handleClick={incrementOne} name="button1" />
      <Button handleClick={incrementTwo} name="button2" />
    </>
  );
};
 
export default Counter;

Now, when you click button1, only the button whose handler changed re-renders:

counter rendered
button1 rendered

Here's why. Clicking button1 changes countOne, which is a dependency of incrementOne, so useCallback returns a new incrementOne function, and button1 re-renders with it. countTwo didn't change, so incrementTwo is the same function as before. button2's props are unchanged, and memo skips re-rendering it.

Make the callbacks stable with updater functions

button1 still re-renders on every click, because incrementOne depends on countOne. You can remove that dependency by passing an updater function to the state setter, which receives the current value:

const incrementOne = useCallback(() => setCountOne((count) => count + 1), []);
const incrementTwo = useCallback(() => setCountTwo((count) => count + 1), []);

With an empty dependency array, both functions are created once and never change. Now, clicking either button re-renders only the counter, and neither button:

counter rendered

The numbers on the screen still update, because they're rendered by Counter, while the buttons themselves don't need to change.

Do you still need useCallback? The React Compiler

Writing memo, useMemo, and useCallback by hand is tedious, and it's easy to get the dependencies wrong. The React Compiler, which reached its stable 1.0 release in October 2025, solves this at build time: it analyzes your components and adds the memoization for you.

We compiled the first version of the example, without memo or useCallback, with the React Compiler's Babel plugin. Clicking button1 printed the same result as the version with memo and useCallback:

counter rendered
button1 rendered

The compiler cached the click handlers and the Button elements automatically, so React skipped re-rendering the button whose props didn't change.

If your project uses the React Compiler, you can write plain components and rarely need useCallback, useMemo, or memo. The React team still recommends them in a few cases, such as when you need precise control over an effect's dependencies, and the compiler works with existing code that already uses them. To try it, follow the installation guide for your build tool or framework.

Summary

useCallback and useMemo are useful tools, but they have specific use cases, and you shouldn't wrap every function in them. On its own, useCallback doesn't prevent a single re-render. Reach for it when you pass a function to a component wrapped in memo, or when a function is a dependency of another Hook, and use updater functions to keep its dependencies short. Better still, let the React Compiler handle memoization for you.

When your React app is ready, you can deploy it for free with Sevalla's Static Site Hosting, or use Application Hosting if it needs a server.

Deep dive into the cloud!

Deploy your application, database, or static site in minutes.