react-usememo
React useMemo
Overview
useMemo caches a computed value between renders. It's an optimization — code should work correctly without it. The common denominator for all valid cases: cost of computation or reference stability.
When to Use
1. Expensive computations
// ❌ Filtering 10,000 products on every render
const filtered = products.filter(p =>
p.name.toLowerCase().includes(query.toLowerCase())
);
// ✅ Recalculates only when products or query change
const filtered = useMemo(() =>
products.filter(p =>
p.name.toLowerCase().includes(query.toLowerCase())
), [products, query]
);
2. Stable reference for memo'd child
Object/array passed as props to a memo() component — without useMemo, a new reference is created every render, defeating memo.
3. Stable reference for useEffect dependency
Object/array used in useEffect deps array — without useMemo, the effect runs every render.
4. Context Provider value
const value = useMemo(() => ({ user, logout }), [user, logout]);
<AuthContext.Provider value={value}>
This is one of the few cases where useMemo is justified without profiling first — the mechanism is well-understood and predictable.
When NOT to Use
| Scenario | Why skip |
|---|---|
Simple operation (price * quantity) |
useMemo overhead > computation cost |
| Primitives (string, number, boolean) | React compares by value, not reference |
| Dependencies change every render | Memoization never caches — just overhead |
| No measured performance problem | Premature optimization obscures code |
| First render | useMemo only helps on re-renders, never first render |
Decision Heuristic
Slow render? → Measure with React DevTools Profiler
↓
Is THIS component the bottleneck?
↓ yes ↓ no
Is computation expensive Look higher up
or result is object/array
passed to memo'd child?
↓ yes
Add useMemo
React Compiler (2025+)
React Compiler auto-memoizes computed values at build time. If you're using it, don't add manual useMemo unless you hit a measured issue or work with uncompiled external libraries. Same rules as useCallback — see react-usecallback for details.
Key Rule
React docs say: check if you have a logic bug first — useMemo on buggy code just hides it. Measure first, optimize second.
References
- useMemo — React docs — official API reference with when-to-use guidance
- Understanding useMemo and useCallback — Josh Comeau's practical guide to memoization decisions
- How to use memo and useCallback — Nadia Makarevich on reference stability
More from b4r7x/agent-skills
react-design-patterns
Use when choosing a React component pattern — custom hooks, control props, compound components, headless components, render props, container/presentational, or other architectural patterns. Includes 13 patterns with decision guide and 2025 popularity ranking.
26human-commit
Generates human-like git commit messages based on staged or unstaged changes. Reads git diff, analyzes what changed, and outputs 3 natural commit message options that sound like they were written by a developer — not AI. This skill should be used when the user wants a commit message, asks "what should I write for commit", "generate commit message", "human like commit", "wiadomość do commita", or just asks for help committing.
24humanize-readme
Rewrites a README.md to remove AI slop — buzzwords, generic openers, fake enthusiasm, and formulaic structure — replacing it with direct, honest, human-sounding writing. This skill should be used when the user wants to humanize a README, remove AI-generated writing patterns, make documentation sound less like ChatGPT wrote it, or asks to "fix the README", "humanize readme", "remove AI slop", "make it sound human".
24improve-prompt
Transforms a rough, unpolished prompt idea into a precise, structured AI coding prompt. Automatically researches the current project context (stack, file structure, conventions, git history) before generating. This skill should be used when the user provides a vague or "dirty" prompt idea and asks to refine, improve, or rewrite it — e.g. "improve this prompt", "refine my prompt", "ulepszony prompt", "dopracuj prompt", or simply describes what they want done in rough terms.
23react-anti-patterns
Use when reviewing React code — especially AI-generated code — to catch common anti-patterns. Covers 18 anti-patterns with detection difficulty, including stale closures, state mutation, useEffect abuse, and boolean explosion.
21deep-plan
Takes a rough, unpolished prompt idea and autonomously turns it into an implementation plan. Researches the project deeply, asks clarifying questions, generates a precise internal prompt, then executes it to produce a structured plan with todos. Designed for plan mode. Use when the user gives a vague feature request, rough idea, or "dirty" prompt and wants a ready-to-execute implementation plan — e.g. "plan this", "deep plan", "turn this into a plan", "zaplanuj to", "zrób plan".
19