Lesson 25/100

Tutorials React.js Tutorial

useRef — Complete Guide

useRef — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of React.js Tutorial on Toolliyo Academy.

On this page

React.js Tutorial · Lesson 25 of 100

useCallback

Beginner ✓IntermediateAdvancedProfessional

Intermediate · 2 — Building apps · ~14 min read · Module 3: Hooks & Component Design

Introduction

You know the basics now. Here we use useCallback in real app situations — forms, pages, and data. Still plain language, just a bit more depth. useCallback returns the same function reference between renders unless dependencies change. Useful when passing callbacks to memoized child components. If you pass a new inline function every render, React.memo on the child cannot skip update the screens.

Hooks look strange at first. Every React developer felt that way. Use the same pattern from this lesson again and again until it feels normal.

When will you use this?

Reach for hooks whenever the screen must react to user input, time, or data from an API.

  • Hooks power live search, dark mode toggles, and fetching data when a page opens.
  • Almost every React job posting mentions hooks — this is core daily work.

Real-world: memoized product grid rows

Amazon product grid wraps each row in React.memo. Parent passes onSelect callback — useCallback keeps function identity stable so memoized rows skip useless re-renders.

Production-style code

const ProductRow = React.memo(function ProductRow({ product, onSelect }) {
  return (
    <tr onClick={() => onSelect(product.id)}>
      <td>{product.title}</td>
      <td>{product.stock}</td>
    </tr>
  );
});

function ProductTable({ products, selectedId, onSelectId }) {
  const handleSelect = useCallback((id) => {
    onSelectId(id);
  }, [onSelectId]);

  return (
    <table>
      <tbody>
        {products.map(p => (
          <ProductRow key={p.id} product={p} onSelect={handleSelect} />
        ))}
      </tbody>
    </table>
  );
}

What happens in production: Scrolling a 5,000-row admin table stays at 60fps because unchanged rows do not re-render when unrelated parent state changes.

Lesson example (start here)

Copy this smaller example first. Once it works, compare it with the real-world code above.

import { useCallback, memo } from 'react';

const Row = memo(function Row({ item, onSelect }) {
  return <tr onClick={() => onSelect(item.id)}><td>{item.name}</td></tr>;
});

function Table({ items }) {
  const handleSelect = useCallback((id) => {
    console.log('Selected', id);
  }, []);
  return items.map(i => <Row key={i.id} item={i} onSelect={handleSelect} />);
}

Line-by-line walkthrough

CodeWhat it means
import { useCallback, memo } from 'react';Imports code from React or another file so you can use it here.
const Row = memo(function Row({ item, onSelect }) {Defines a function — often a component or event handler.
return <tr onClick={() => onSelect(item.id)}><td>{item.name}</td></tr>;Defines a function — often a component or event handler.
});Closes a block started by { or ( above.
function Table({ items }) {Defines a function — often a component or event handler.
const handleSelect = useCallback((id) => {Defines a function — often a component or event handler.
console.log('Selected', id);Part of the useCallback example — read it together with the lines before and after.
}, []);Closes a block started by { or ( above.
return items.map(i => <Row key={i.id} item={i} onSelect={handleSelect} />);Defines a function — often a component or event handler.
}Closes a block started by { or ( above.

How it works (big picture)

  • handleSelect keeps the same reference so memoized Row avoids useless update the screens.
  • Empty deps [] if it does not use changing values.

Do this on your computer

  1. Use React DevTools to see why a child update the screens.
  2. Wrap stable callbacks with useCallback.
  3. Only optimize after confirming it helps.
  4. Read the real-world section and name which part of the app uses this topic.
  5. Run the example locally and confirm the same behavior in the browser.
  6. Change one value in the example (text, initial state, or URL) and predict what will happen before you save.

Experiments — try changing this

  • Change text or labels in the example and save — watch the browser update.
  • Break the code on purpose (remove a bracket), read the error message, then fix it.
  • Add one more item to the array in the example and confirm it appears in the list.
  • Open React DevTools (browser extension) while running useCallback and inspect component props/state.

Remember

useCallback(fn, deps) stabilizes function identity. Pair with React.memo on children when needed.

Common questions

Do beginners need useCallback?

Usually no — learn it when optimizing lists or memoized components.

How long should I spend on useCallback?

Until you can explain it in your own words and run the example without looking at the answer. Beginners often need 30–60 minutes per new hook or routing topic; setup lessons may take one afternoon.

What if I get stuck on useCallback?

Re-read the line-by-line walkthrough, check the browser console for red errors, and compare your code character-by-character with the example. Search the exact error text — someone else had it too.

Where is useCallback used in real jobs?

See the real-world section above — the same pattern appears in LMS, banking, e-commerce, and SaaS products. Interviewers ask you to explain it using one concrete example from your project or this lesson.

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

React.js Tutorial
Course syllabus

React.js Tutorial

Module 1: React Basics & Setup
Module 2: Props, Events & Lists
Module 3: Forms & Hooks
Module 4: Routing & Data
Module 5: State & Authentication
Module 6: Architecture & React 19
Module 7: Performance
Module 8: Full-Stack & Real-Time
Module 9: Testing & Deployment
Module 10: ShopCart Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details