memo, useMemo, useCallback and the React Compiler
memo, useMemo and useCallback let React skip work when nothing relevant changed: memo skips a child's re-render, useMemo caches a value and useCallback caches a function. They only help when used correctly, and the React Compiler can now add this memoization automatically.
37.1 The problem: an unnecessary re-render
- App has a likes state and renders ProductList with a products list that never changes.
- Clicking Likes logs ProductList render again even though products were exactly the same.
- Default rule: when a component re-renders, all its children re-render, whether their props changed or not.
- Usually fine (renders are fast, only changed parts hit the DOM); a problem only when the child is heavy, like a 1000-item list or a chart.
import { useState } from "react";import ProductList from "./ProductList";import { products } from "./data"; export default function App() { const [likes, setLikes] = useState(0); return ( <> <button onClick={() => setLikes(likes + 1)}> Likes: {likes} </button> <ProductList products={products} /> </> );}> ProductList render-- click Likes --> ProductList renderScreen: Likes 137.2 Fix: memo
- memo wraps a component and tells React not to re-render it if its props are the same.
- The ProductList function stays the same; only the export becomes memo(ProductList).
- Now clicking Likes shows Likes 1 but ProductList render appears only once (from the first render).
import { memo } from "react"; function ProductList({ products }) { console.log("ProductList render"); return ( <ul> {products.map((p) => ( <li key={p.id}>{p.name}</li> ))} </ul> );} export default memo(ProductList);> ProductList render-- click Likes --Screen: Likes 137.3 Why memo fails: props compared by reference
- Shop filters phones below max and passes the result to the memoized ProductList.
- Clicking Likes logs ProductList render again: filter creates a new array every render.
- memo compares each prop with Object.is, which checks only the reference for arrays, objects and functions.
- A new array with the same content still counts as a different prop.
export default function Shop({ max }) { const [likes, setLikes] = useState(0); const cheap = products.filter( p => p.price < max ); return ( <> <button onClick={() => setLikes(likes + 1)}> Likes: {likes} </button> <ProductList products={cheap} /> </> );}> ProductList render-- click Likes --> ProductList renderScreen: Likes 1- Step 1
1.Likes click: Shop's state changes
- Step 2
2.Shop re-renders: products.filter() makes a NEW array
- Step 3
3.memo compares old vs new props with Object.is
- Step 4
4.Different reference -> ProductList re-renders
- Step 5
5.Same reference (useMemo) -> render skipped
memo only works when props are stable
37.4 Fix: useMemo
- useMemo remembers the result of a calculation between renders.
- Put the filter in a function given to useMemo with [max] as dependencies.
- React re-runs the filter only when max changes; otherwise it returns the previous array with the same reference.
- Clicking Likes no longer logs a new render: memo works again.
export default function Shop({ max }) { const [likes, setLikes] = useState(0); const cheap = useMemo( () => products.filter(p => p.price < max), [max] ); return ( <> <button onClick={() => setLikes(likes + 1)}> Likes: {likes} </button> <ProductList products={cheap} /> </> );}> ProductList render-- click Likes --Screen: Likes 137.5 For functions: useCallback
- In JavaScript a function is also an object, so an inline arrow passed to onSelect is new on every render and memo fails.
- useCallback remembers the function itself and returns the same function while its dependencies stay the same.
- useMemo caches a value; useCallback caches a function.
// Wrong: new function every render -> memo fails<ProductList products={cheap} onSelect={id => setPicked(id)}/>// Right: same function, render skippedconst select = useCallback( id => setPicked(id), []); <ProductList products={cheap} onSelect={select}/>37.6 Checkpoint: one unstable prop breaks memo
- ProductList is memoized and cheap comes from useMemo, but style gets an inline object.
- Clicking Likes re-renders ProductList: the style object is new every render.
- A single unstable prop makes the whole memo useless.
<ProductList products={cheap} style={{ color: "green" }}/>> ProductList render> ProductList renderScreen: Likes 137.7 Comparison: memo vs useMemo vs useCallback
| Tool | What it caches | Applied to | Use when |
|---|---|---|---|
| memo | The rendered output of a component | A component (wrap the export) | Heavy child whose props mostly stay the same |
| useMemo | A calculated value | A value inside a component | Expensive calculation, or keeping an object/array reference stable |
| useCallback | A function | A function inside a component | Function goes to a memo child or into effect deps |
| React Compiler | All of the above, automatically | The whole build | New projects; then manual memo only when the Profiler says so |
37.8 Common mistakes
- Missing dependency in useMemo: with [] the filter runs only on the first render; when max changes from 20,000 to 1,00,000 the list stays old and the iPhone never shows up.
- Fix: put every prop or state the function uses into the dependency array (here max).
// Wrong: max changes, list stays oldconst cheap = useMemo( () => products.filter(p => p.price < max), []);// Right: max changes, list is freshconst cheap = useMemo( () => products.filter(p => p.price < max), [max]);- Myth: "memo everything and the app gets faster". Memoization is not free: React compares dependencies every render and keeps old values in memory.
- If the child is light, or its props change every time anyway, memo is only extra code and work. Measure first with the Profiler in React DevTools.
37.9 The React Compiler
- Stable version 1.0 came out in October 2025.
- A build-time tool that analyzes your components and adds memoization (like memo, useMemo, useCallback) by itself.
- Next.js 16: install babel-plugin-react-compiler as a dev dependency and set reactCompiler: true in next.config.ts.
- Vite: the same via a Babel plugin; it can also work with React 17 and 18.
npm install -D babel-plugin-react-compilernpm run devimport type { NextConfig } from "next"; const nextConfig: NextConfig = { reactCompiler: true,}; export default nextConfig;- Step 1
1.You write plain components (no manual memo)
- Step 2
2.Build: babel-plugin-react-compiler analyzes each component
- Step 3
3.Compiler inserts memoization for values, functions and JSX
- Step 4
4.Bundle ships with auto-memoized components
- Step 5
5.Re-renders skip unchanged work automatically
37.10 What to use when
- Turn on the React Compiler in new projects; write manual memo only when the Profiler shows a really slow component.
- memo: heavy child whose props mostly stay the same.
- useMemo: an expensive calculation, or keeping an object's reference stable.
- useCallback: only when the function goes to a memo child or into an effect's dependencies.
37.11 Interview tips
- memo wraps a component and skips its re-render when props are the same; useMemo caches a value; useCallback caches a function.
- Props are compared by reference (Object.is), so memo only works when objects and functions passed in are stable.
- Memoization has a cost; measure with the React DevTools Profiler before adding it.
- The React Compiler (1.0, October 2025) now does this memoization automatically at build time.