Lesson 168 of 296
useMemo and useCallback
What you will learn
- Referential equality and why it matters
- useMemo for expensive calculations and stable objects
- useCallback for stable functions
- When memoisation helps and when it is wasted
- The React Compiler
On this page
The problem: new objects every render
Every render runs the component function again, so every object, array and function created inside it is new:
const options = { sort: "price" }; a new object each render
const handleClick = () => {}; a new function each renderThat matters in three situations:
- A child wrapped in
React.memo(Lesson 172) compares props by reference; a new function or object prop makes it re-render anyway. - A
useEffectdepends on that object or function, so it runs after every render (Lesson 166). - A calculation is expensive (sorting or filtering thousands of items) and repeats on every render.
useMemo: remember a calculated value
import { useMemo } from "react";
type Row = { id: string; name: string; pricePence: number };
function ProductSearch({ rows, query }: { rows: Row[]; query: string }) {
const visibleRows = useMemo(() => {
const q = query.trim().toLowerCase();
return rows
.filter((r) => r.name.toLowerCase().includes(q))
.sort((a, b) => a.pricePence - b.pricePence);
}, [rows, query]);
return <ul>{visibleRows.map((r) => <li key={r.id}>{r.name}</li>)}</ul>;
}React runs the function on the first render, and again only when a dependency changes. Otherwise it returns the remembered result.
useCallback: remember a function
import { useCallback } from "react";
function TodoPage({ onSave }: { onSave: (title: string) => void }) {
const [title, setTitle] = useState("");
const handleSave = useCallback(() => {
onSave(title);
setTitle("");
}, [onSave, title]);
return <button type="button" onClick={handleSave}>Save {title}</button>;
}useCallback(fn, deps) is the same as useMemo(() => fn, deps): the same function object is returned until a dependency changes.
When memoisation helps
| Use it when | Example |
|---|---|
| The calculation is measurably slow | Filtering or sorting thousands of rows, heavy maths |
The value is passed to a React.memo child | Stable props let the child skip rendering |
| The value is a dependency of an effect or another hook | Prevents the effect running every render |
| A context provider value | Prevents every consumer re-rendering (Lesson 169) |
When it is wasted
- Cheap calculations (
a + b, building a small string): memoising costs more than recalculating. - Functions passed to plain DOM elements (
<button onClick>): DOM elements do not care about identity. - Wrong or missing dependencies: you get stale values, a real bug.
Measure first with the React Profiler (Lesson 172). Memoisation is an optimisation, not a default.
The React Compiler
The React Compiler (stable 1.0 released in October 2025) is a build-time tool that automatically memoises components and values where it is safe. In projects that use it, you rarely need to write useMemo, useCallback or React.memo by hand. You still need to understand them: for existing code, for projects without the compiler, and for interviews. The compiler also relies on your code following the Rules of React (pure rendering, no mutating props or state).
How to say it in an interview
"Every render creates new objects and functions, which breaks reference equality for memoised children and for effect dependencies. useMemo caches a computed value and useCallback caches a function until their dependencies change. I use them for expensive calculations, props passed to React.memo components, dependencies of effects, and context values, but not for cheap work, and I measure with the Profiler first. The React Compiler now automates most of this in projects that enable it."
Key words
| Word | Meaning |
|---|---|
| Memoisation | Remembering a result to avoid recalculating it |
| Referential equality | Two values being the very same object |
| useMemo | Remembers a calculated value |
| useCallback | Remembers a function |
| React Compiler | A build tool that memoises automatically |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.