askill
react-usememo

react-usememoSafety 85Repository

Use when writing or reviewing useMemo usage in React components. Covers the 4 valid cases, when to skip it, and the practical heuristic for deciding.

0 stars
1.2k downloads
Updated 3/13/2026

Package Files

Loading files...
SKILL.md

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

ScenarioWhy skip
Simple operation (price * quantity)useMemo overhead > computation cost
Primitives (string, number, boolean)React compares by value, not reference
Dependencies change every renderMemoization never caches — just overhead
No measured performance problemPremature optimization obscures code
First renderuseMemo 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

Install

Download ZIP
Requires askill CLI v1.0+

AI Quality Score

87/100Analyzed 3/27/2026

High-quality reference skill covering React useMemo comprehensively. Provides clear when-to-use/when-not-to-use guidance with practical decision heuristic, code examples, and table format. Actionable and well-structured despite misaligned tags (ci-cd doesn't fit). Scores high on reusability as general React guidance.

85
90
90
85
90

Metadata

Licenseunknown
Version-
Updated3/13/2026
Publisherb4r7x

Tags

apici-cd