ReactMedium

When to use memoization?

Memoize when you have measured a real performance problem: an expensive calculation that re-runs needlessly, or a slow child component that re-renders with identical props. Otherwise prefer simpler fixes, and in projects using the React Compiler let it memoize automatically.

Good reasons

  • Profiler shows a component or computation taking significant time on frequent renders.
  • Large lists or tables where each row re-renders on unrelated state changes.
  • Stable references required: objects or functions used in effect dependencies, context values, or props to React.memo components.
  • Expensive derived data: sorting or filtering thousands of items, heavy formatting, parsing.

Reasons to skip it

  • The work is cheap (under about 1ms): memoization has its own overhead and adds noise.
  • The props change on every render anyway.
  • The real problem is architecture.

Fix structure first

  1. Move state down: keep fast-changing state (input text, hover) in the smallest component that needs it.
  2. Lift content up: pass expensive subtrees as children so they are not re-created by the stateful wrapper.
  3. Split context so consumers only re-render for values they use.
  4. Virtualize long lists.
  5. Use transitions (useTransition, useDeferredValue) so heavy updates do not block typing.

React Compiler

Version 1.0 (October 2025) memoizes components, JSX and values automatically and more precisely than hand-written code. It is available in Vite templates (react-compiler-ts), Next.js (reactCompiler: true) and Expo. With it, manual memo, useMemo and useCallback become rare escape hatches.

Code example

tsx
1import { useState } from "react";
2
3// Instead of memoizing, move fast-changing state down
4function SearchInput({ onSearch }: { onSearch: (q: string) => void }) {
5 const [text, setText] = useState(""); // typing re-renders only this component
6 return (
7 <form onSubmit={(e) => { e.preventDefault(); onSearch(text); }}>
8 <input value={text} onChange={(e) => setText(e.target.value)} />
9 </form>
10 );
11}
12
13function ExpensiveReport({ query }: { query: string }) {
14 // imagine heavy rendering here
15 return <p>Report for “{query}”</p>;
16}
17
18export function Dashboard() {
19 const [query, setQuery] = useState("");
20 return (
21 <>
22 <SearchInput onSearch={setQuery} />
23 <ExpensiveReport query={query} /> {/* re-renders only on submit, not on every keystroke */}
24 </>
25 );
26}
27
28// With the React Compiler enabled (vite template react-compiler-ts, or Next.js reactCompiler: true),
29// components like these are memoized automatically at build time.

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app