Sitemap
Learn React

Welcome to Learn React! This Medium blog delivers TL;DR-style articles on React and its ecosystem.

useCallback() in React

3 min readAug 5, 2026

--

Press enter or click to view image in full size
Photo by Andres Medina on Unsplash

Introduction

When you pass a function as a dependency to useEffect(), you might encounter issues if the function is recreated on every render. This can lead to unnecessary re-renders or even infinite loops. To avoid this, you can use useCallback() to memoize the function, ensuring that it only changes when its dependencies change.

Understanding the Problem

To understand the problem, consider the following example:

import { useEffect, useState } from "react";

export function InfinityLoop() {
const [count, setCount] = useState(0);

function prettyPrint() {
console.log("Pretty Print");
}

useEffect(() => {
prettyPrint();
setCount((prevCount) => prevCount + 1);
}, [prettyPrint]);

return <h1>Current Count : {count}</h1>;
}

In this example, useEffect() calls both prettyPrint() and setCount(). Since prettyPrint() is used inside the effect, it is included in the dependency array. However, when you run this code, the count state increases indefinitely, resulting in an infinite loop.

Get Ye Min Ko’s stories in your inbox

Join Medium for free to get updates from this writer.

This happens because calling setCount() updates the state, which causes the component to re-render. On every re-render, React creates a new prettyPrint() function. Because the function reference has changed, React considers the dependency to have changed, so it runs the effect again. The effect then calls setCount(), triggering another re-render, and the cycle repeats.

Note: Every time a component re-renders, React recreates all functions declared inside the component. This means a function such as prettyPrint() will have a new reference on every render. If you include such a function in a useEffect() dependency array, the effect will run after every render because the function reference is always different.

Solution

To solve this problem, you can use the useCallback() hook to memoize the function. This ensures that the function reference remains the same across renders unless its dependencies change. Here's how you can modify the previous example:

import { useEffect, useState, useCallback } from "react";

export function InfinityLoop() {
const [count, setCount] = useState(0);

const prettyPrint = useCallback(() => {
console.log("Pretty Print");
}, []); // No dependencies, so the function reference will remain the same

useEffect(() => {
prettyPrint();
setCount((prevCount) => prevCount + 1);
}, [prettyPrint]);

return <h1>Current Count : {count}</h1>;
}

useCallback() takes two arguments: the function you want to memoize and an array of dependencies. In this case, the dependency array is empty, meaning that prettyPrint() will not change unless the component is unmounted and remounted.

When to Use useCallback()

You should use useCallback() when you want to prevent a function from being recreated on every render, especially when that function is used as a dependency in useEffect(). This can help optimize performance and avoid unnecessary re-renders.

Conclusion

In summary, useCallback() is a useful React hook that memoizes a function so it isn't recreated on every render. You'll often use it when passing a function as a dependency to useEffect(), helping you avoid unnecessary effect executions. It also works well with React.memo() to reduce unnecessary re-renders of child components that receive callback functions as props. We'll explore this optimization in this article.

--

--

Ye Min Ko
Ye Min Ko

Written by Ye Min Ko

My knowledge, my books, and nature are the pillars of my existence.

Learn React
Learn React

Published in Learn React

Welcome to Learn React! This Medium blog delivers TL;DR-style articles on React and its ecosystem.