useCallback

React chapter 13 · useCallback · useEventCallback · source: react.dev/reference/react/useCallback

The problem

The row is wrapped in memo and its text never changes, but the parent passes onAdd={() => ...} inline. A function written in the body is a new function every render, so memo sees a change.

The pattern that fixes it

useCallback(fn, deps) hands back the same function until its dependencies change. Use the state updater form so the function needs no dependencies and stays stable forever.

In plain words

A function you write inside a component is created again on every render. To React, the new copy is a different thing from the old one, even if the code is identical. useCallback keeps the old copy.

Every morning you hand each cashier a freshly printed copy of the same instruction sheet. They think the rules changed and stop to re-read it. useCallback gives them the same sheet until the rules really change.

Terms

callback
A function you pass to a child so the child can tell the parent something happened, like onAdd.
closure
A function remembers the variables from the render where it was created. An old function remembers old values.
updater function
Passing setCart(c => [...c, id]) instead of a value. React supplies the latest state, so you do not need to read cart yourself.

Going deeper

How does it work?

useCallback(fn, deps) is the same as useMemo(() => fn, deps). On each render React compares the dependency list with last time using Object.is. If nothing changed it returns the stored function, otherwise it stores and returns the new one. A memoized child then sees the same prop and skips rendering.

When it does nothing, or goes wrong

It does nothing if the child is not wrapped in memo, because the child re-renders anyway. Putting cart in the deps keeps the function correct but makes it change on every cart update (try it in the lab). Leaving cart out while reading it gives a stale closure: the function keeps using the cart from the first render. The updater form setCart(c => ...) avoids both problems.

When NOT to use it, and modern alternatives

useCallback itself makes nothing faster; it only matters when something compares function identity (a memo child, an effect dependency). Do not wrap every handler. When a handler must always see the latest values without changing identity, use useEffectEvent (or MUI useEventCallback), covered in chapter 15. The React Compiler memoizes handlers automatically, so most manual useCallback calls can go.

Keep learning

Overview

Tick the clock (rows do not care) Owns the cart state and passes onAdd down to each row. Real life: a shopping list where every product row has an Add button.

Tick the clock: all four memo rows re-render.

Press Tick the clock three times and watch the four Row badges rise together. Press Add Tea. The cart works, but every row re-renders because the Page did. Same shopping list. Make onAdd stable, then try the two common mistakes. How onAdd is written Inline (no useCallback) useCallback + updater [] useCallback + [cart] useCallback + [] reading cart new each render reads cart, but cart is not in deps

Inline: every clock tick re-renders all four rows.

Switch to the second option and tick again.

Stable but wrong: the cart gets stuck at one item.

Use useCallback when The child is wrapped in memo. It sits in a context value. Skip it when The child is a plain element like <button>. The React Compiler is on. On the second option, press Add Tea twice, then Tick the clock. Only Page changes. On the last option press Add Tea, then Add Rice. The cart still holds one item.

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.