useMemo

React chapter 12 · useMemo · theme, context value, ownerState · source: react.dev/reference/react/useMemo

The problem

The child is wrapped in memo, but the parent builds a fresh object or list on every render. A fresh object never equals the old one, so memo never skips anything.

The pattern that fixes it

useMemo remembers a value and hands back the very same object until its dependencies change. Now memo can do its job, and expensive filtering only runs when its inputs change.

In plain words

Every time a component runs, it rebuilds every object and list it creates, even if the contents are identical. Two objects that look the same are still different things in memory. useMemo says: "keep the one you already made, unless these inputs changed."

You print a fresh copy of the same menu every morning, so the waiter thinks it is a new menu each time. useMemo keeps yesterday's menu on the table until the dishes actually change.

Terms

reference (identity)
Which exact object in memory you are holding. {a:1} and another {a:1} look alike but are two different references.
dependencies
The list at the end, like [name]. If none of them changed since last render, React reuses the stored value.
derived data
A value you compute from state, such as a filtered list. You do not store it, you recalculate it.
stale value
An old result that no longer matches the current inputs, usually from a missing dependency.

Going deeper

How does it work?

On each render React compares the new dependency array with the previous one using Object.is, item by item. If all match, useMemo returns the value it stored last time and does not call your function. If one differs, it calls the function and stores the new result. The stored value lives with the component, so each component instance has its own cache.

When it does nothing, or goes wrong

It does nothing if the child is not wrapped in memo, because the child re-renders with its parent anyway. It also does nothing if a dependency is itself a new object each render. Leaving out a dependency (try the third option in the lab) gives you stale values, which is worse than being slow. React may also discard cached values in rare cases, so never rely on it for correctness, only for speed.

When NOT to use it, and modern alternatives

Do not wrap cheap work: reading the cache costs about as much as a small filter. Use it where a reference must stay stable (a memo child prop, a context value, an effect dependency) or where a calculation is genuinely slow. The React Compiler adds this kind of memoization automatically, and with it on you can delete most hand-written useMemo calls. Measure with the React DevTools Profiler first.

Keep learning

Overview

Wrapped in memo. It should only render when the name changes. Wrapped in memo. It should only render when the search text changes. Tick the clock (nothing the children show) Change the user name Search the 2,000 items try: fig State lives here. It builds the user object and the filtered list, then hands them down. Real life: a shopping page with a clock in the corner, a profile chart, and a search over 2,000 products.

Tick the clock: both memo badges still climb.

Both children are wrapped in memo, yet the clock tick re-renders them. Each tick builds a new object and runs the 2,000-item filter again, and a new reference always counts as "changed". Press Tick the clock four times. Watch Chart and Results badges rise although you changed nothing they show. Type fig in the search. The filter is real work, and it also reruns on every clock tick. Same page. Switch useMemo on, then break it with a missing dependency. How the values are built No useMemo useMemo + correct deps useMemo + empty deps [] rebuilt only when name changes never rebuilt

Without useMemo, a clock tick re-renders both memo children.

Switch to the middle option and tick again.

Tick the clock: Chart and Results stay still.

The same object and the same array come back, so memo skips them. Change the name or type in the search and exactly the matching child renders, because that is real new input.

Fast, but wrong: the values are stale.

With [] React never rebuilds the value, so change the name or search and the children keep showing the first answer. Every value you read inside must be in the dependency list. Reach for useMemo when A memo child, context value or effect needs a stable reference. A calculation is truly slow (big filter, sort, parse). Skip it when The work is tiny, like adding two numbers. The child is not wrapped in memo. The React Compiler already handles it. With the middle option, tick the clock five times, then press Change the user name: only Chart moves. Switch to empty deps, press Change the user name and compare Page with Chart. That is a stale value.

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.