-
ReactJS useCallback Hook
-
In React, functions are re-created every time a component re-renders. While this behavior is normal, it can sometimes cause unnecessary re-renders in child components, especially when functions are passed as props. The
useCallbackHook helps solve this problem by preserving function references between renders.
What Is useCallback?
useCallbackis a React Hook that returns a memoized version of a function. code>useCallback allows you to store and reuse the same function reference between renders instead of creating a new function every time a component re-renders.In React, a component is a JavaScript function. Each time it re-runs, all functions declared inside it are recreated in memory, even if their logic has not changed.
useCallbackprevents this unnecessary recreation by memoizing the function itself.useCallbacktells React to remember a function and reuse it until its dependencies change.
Importing useCallback
import { useCallback } from "react";
Basic Syntax Of useCallback
Syntax without Arguments
What This Meansconst memoizedFunction = useCallback(() => { // function logic }, [dependencies]);- The function is created once
- React stores the function reference
- A new function is created only when dependencies change
Arguments:useCallback(callback, dependencies)
1. callback- The function you want React to remember.
- An array of values the function depends on.
- The function will be recreated only when one of these values changes.
Example 1: Without useCallback
Parent Component
Explanation:import { useState } from "react"; import Child from "./Child"; function Parent() { const [count, setCount] = useState(0); // Function is recreated on every render const incrementCount = () => { setCount(count + 1); }; return ( <div> <h2>Parent Component</h2> <p>Count: {count}</p> <Child onIncrement={incrementCount} /> </div> ); } export default Parent;- Parent component renders
incrementCountfunction is created- Parent state updates
- Parent re-renders
- A new
incrementCountfunction is created again - Child receives a new function reference
function Child({ onIncrement }) { return ( <div> <h3>Child Component</h3> <button onClick={onIncrement}> Increment from Child </button> </div> ); } export default Child;
Example 2 : With useCallback
Parent Component
Explanation:import { useState, useCallback } from "react"; import Child from "./Child"; function Parent() { const [count, setCount] = useState(0); // Function is memoized using useCallback const incrementCount = useCallback(() => { setCount(prevCount => prevCount + 1); }, []); return ( <div> <h2>Parent Component</h2> <p>Count: {count}</p> <Child onIncrement={incrementCount} /> </div> ); } export default Parent;incrementCountis created usinguseCallback
React stores the function reference
- The same function is passed to the child on every render
- Parent state updates when the function is called
function Child({ onIncrement }) { return ( <div> <h3>Child Component</h3> <button onClick={onIncrement}> Increment from Child </button> </div> ); } export default Child;
Real-World Scenarios Where useCallback Is Useful
-
Passing click handlers from a parent component to child components
When a parent re-renders, the click handler functions are recreated.useCallbackkeeps these handlers stable and avoids unnecessary updates. -
Dashboard pages with multiple widgets receiving action functions
Widgets often receive callback functions from a container component.useCallbackhelps prevent widget re-renders caused by changing function references. -
Form components passing submit or change handlers to input fields
When typing in forms, frequent re-renders occur.useCallbackprevents input components from re-rendering due to recreated handlers. -
Rendering large lists where each item receives the same callback
Passing a newly created function to each list item can impact performance.useCallbackensures a single stable function reference is reused. -
Child components sending data back to the parent using callbacks
Even in child-to-parent communication, the callback is defined in the parent.useCallbackkeeps that function reference consistent across renders. -
Parent components that re-render frequently due to state changes
Frequent parent updates can cause unnecessary function recreation.useCallbackprevents child components from being affected by these updates. -
Using the same event handler across multiple UI elements
Reusing a memoized callback avoids creating multiple function instances for the same logic. -
Functions used inside other hooks like useEffect or useMemo
Changing function references can trigger unwanted re-execution.useCallbackkeeps dependencies stable. -
Complex pages with deeply nested component structures
In large applications, unnecessary re-renders become noticeable.useCallbackhelps reduce wasted rendering work. -
Reusable component libraries where prop stability matters
Stable callback references make reusable components more predictable and efficient.
Final Summary
The
Rule to RememberuseCallbackHook helps optimize React applications by preventing unnecessary function recreation during re-renders. It is especially useful when functions depend on state or props and are reused across renders. By memoizing callback functions and recreating them only when their dependencies change,useCallbackimproves performance without altering application behavior.If you want React to remember a function, use useCallback.If you want React to remember a value, use useMemo.