KoderSolution Logo
HomeArticlesTutorialsForumAI LabRun Code
KoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Engineering Newsletter

Join 100,000+ engineers receiving curated high-signal content weekly.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
⚛️

React

Topic Hub & Articles

React Intro

10 min

Recap Quiz

5 Questions

React Getting Started

10 min

React ES6

10 min

React Render HTML

10 min

Recap Quiz

5 Questions

React JSX

10 min

React Components

10 min

React Class Components

10 min

Recap Quiz

5 Questions

React Props

10 min

React Events

10 min

React Conditionals

10 min

Recap Quiz

5 Questions

React Lists

10 min

React Forms

10 min

React Router

10 min

Recap Quiz

5 Questions

React Memo

10 min

React CSS Styling

10 min

React Sass Styling

10 min

Recap Quiz

5 Questions

React Fragments

10 min

React Portals

10 min

React Profiler

10 min

Recap Quiz

5 Questions

React Strict Mode

10 min

React Higher Order Components

10 min

React Context API

10 min

React Error Boundaries

10 min

What is a Hook

10 min

Recap Quiz

5 Questions

useState

10 min

useEffect

10 min

useContext

10 min

Recap Quiz

5 Questions

useRef

10 min

useReducer

10 min

useCallback

10 min

Recap Quiz

5 Questions

useMemo

10 min

Custom Hooks

10 min

useLayoutEffect

10 min

Recap Quiz

5 Questions

useImperativeHandle

10 min

useDebugValue

10 min

useDeferredValue

10 min

Recap Quiz

5 Questions

useTransition

10 min

useId

10 min

Progress
0%

0 / 38 Lessons

ReactReact Hooks
Lesson

useCallback

10 min reading
Free Course

useCallback: Memoizing Event Handlers & Callback Functions

The useCallback hook returns a memoized version of a callback function that only changes when one of its specified dependencies changes.

In JavaScript, functions are first-class objects. Every time a component re-renders, any inline arrow functions or callback definitions inside its body are recreated with new memory references. Passing un-memoized callbacks to memoized child components (React.memo) breaks shallow comparison and causes unwanted child re-renders.

Function Reference Memoization

flowchart TD
    Parent["Parent Re-renders"] --> Check{"Have Dependencies Changed?"}
    Check -- No --> Keep["Return Cached Function Reference"]
    Check -- Yes --> Recreate["Re-create New Function Reference"]

Practical Code Example

import React, { useState, useCallback, memo } from 'react';

// Child component memoized with React.memo
const FilterButton = memo(({ category, onClick }) => {
  console.log(`FilterButton [${category}] rendered!`);
  return (
    <button onClick={() => onClick(category)} className="btn">
      Filter by {category}
    </button>
  );
});

export default function ProductCatalog() {
  const [selectedCategory, setSelectedCategory] = useState('All');
  const [searchTerm, setSearchTerm] = useState('');

  // useCallback memoizes function reference across renders
  const handleCategorySelect = useCallback((category) => {
    setSelectedCategory(category);
  }, []); // Empty dependency array: Function reference remains identical forever

  return (
    <div className="catalog">
      <h2>Selected: {selectedCategory}</h2>

      {/* Typing in search updates search state, but FilterButton will NOT re-render */}
      <input 
        type="text" 
        value={searchTerm} 
        onChange={(e) => setSearchTerm(e.target.value)} 
        placeholder="Type to search..." 
      />

      <div className="button-group">
        <FilterButton category="Electronics" onClick={handleCategorySelect} />
        <FilterButton category="Books" onClick={handleCategorySelect} />
      </div>
    </div>
  );
}

Best Practices & Gotchas

  • Pair useCallback with React.memo: Using useCallback by itself on a function passed to an unmemoized native element (<button onClick={fn}>) provides zero rendering optimization. Use useCallback primarily when passing callbacks to React.memo components or useEffect dependencies.
  • Include All Referenced Variables in Dependencies: Ensure any state or prop variables accessed inside the callback are listed in the dependency array to avoid stale closures.

Self-Check Challenge

Explain why useCallback(fn, deps) is equivalent to useMemo(() => fn, deps).

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Recommended Courses & Books

Lesson Recap Quiz Available

Test Your Knowledge

You've completed this section! Take a quick 5-question quiz to check your understanding.

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum