memo
React chapter 11 · React.memo · ButtonBase, ListItem · source: react.dev/reference/react/memo
The problem
A parent re-render re-renders every child. The clock ticks, so the heavy chart redraws even though nothing it shows has changed.
The pattern that fixes it
memo skips a child when its props are shallow-equal to last time. The parent can still tick; the chart only renders when its own props change. MUI memoizes many leaf components the same way.
In plain words
When a component runs again, React also runs every component inside it, even the ones whose inputs did not change. memo is a doorman for one component: "did my props change? No? Then I skip this round."
A manager reprints the whole report whenever any single number changes. memo tells one clerk: only redo your page if your own page changed.
Terms
- render
- React calls your component function to find out what the screen should look like. It is not the same as painting pixels.
- props
- The inputs a parent hands to a child, like title="Sales".
- shallow compare
- React checks each prop with Object.is: same number or text, or the very same object in memory.
Going deeper
How does memo decide?
Before running the child, React compares each new prop to the previous prop with Object.is. All equal means it reuses the last result and skips the function call. One different prop means a normal render. The check is cheap but not free, so it pays off only for components that are expensive to render or render very often.
When memo silently does nothing
A new object, array or function is never Object.is-equal to the old one, even if it holds the same data. <Chart options={{ a: 1 }} /> creates a fresh object on every parent render, so the memo check always fails. Fix it with useMemo for values (next chapter) and useCallback for functions (the one after).
When not to use it
Skip memo on cheap components, on components whose props change almost every time, and when the React Compiler is on, because the compiler adds this kind of memoization automatically. Measure first with the React DevTools Profiler.
Keep learning
- Previous: Keep mounted / exit transition
- Next: useMemo
- Related: useMemo
- Related: useCallback
- Related: Custom hooks
- Related: useEffectEvent
- Related: useTransition
- Related: useDeferredValue
Overview
Pretend this costs 12 ms every time it renders. Same data, but a brand-new object each time. Needs the name, so it should re-render when it changes. Tick the clock (parent changes, chart does not care) Change the name Change the chart title State lives here. Every button above changes Page's state. Real life: a dashboard page with a clock in the corner and a heavy chart below it.
Look at the Chart badge after ticking the clock.
The title is still "Sales", yet Chart keeps rendering. React's default rule is simple: if the parent rendered, every child renders. Press Tick the clock five times. Watch Header and Chart both flash. Press Change the name. Header needs it, Chart does not, but Chart renders anyway. Same page. Flip memo on, then try to break it with a brand-new object prop. Is Chart wrapped in memo? No memo
Sales
, hot: variant === same text each time new object every render
memo is on, but it still renders.
The data inside the object never changes, but a fresh object is a new identity, so the shallow compare says "different". Next chapter fixes this with useMemo.
Tick the clock: Chart stays still.
Its props are identical, so React skips it. Change the chart title and it renders, because that really is new input. Use memo when The component is slow to render. It renders often with the same props. Props are text, numbers or stable references. Skip memo when It is cheap anyway. Its props change on nearly every render. You pass fresh objects or functions (fix those first).
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.