Skip to main content

Lesson 2 of 10

Memoization - React.memo, useMemo, useCallback

Three memoization tools, each solving a different problem: React.memo prevents a child from re-rendering when its props haven't changed. useMemo caches an expensive computed value. useCallback caches a function reference so memoized children don't see a new prop every render.

35 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

When Memoization Actually Helps

When Memoization Actually Helps
import { memo, useMemo, useCallback, useState } from 'react';

// SCENARIO: Parent state updates shouldn't re-render an expensive child

// ✅ Step 1: Wrap expensive child with React.memo
const ExpensiveChart = memo(function ExpensiveChart({
  data,
  onPointClick,
}: {
  data: DataPoint[];
  onPointClick: (point: DataPoint) => void;
}) {
  // Expensive: renders thousands of SVG elements
  return <svg>{data.map(p => <circle key={p.id} cx={p.x} cy={p.y} />)}</svg>;
});

function Dashboard() {
  const [filter, setFilter] = useState('all');
  const [rawData] = useState<DataPoint[]>(generateLargeDataset());

  // ✅ Step 2: Memoize the filtered data - only recomputes when filter or rawData changes
  const filteredData = useMemo(
    () => rawData.filter(p => filter === 'all' || p.category === filter),
    [rawData, filter]
  );

  // ✅ Step 3: Stable callback - won't change between renders (no deps → never changes)
  const handlePointClick = useCallback((point: DataPoint) => {
    console.log('clicked:', point);
    // If you needed state here: include it in deps or use useRef for the handler
  }, []);

  return (
    <>
      <select value={filter} onChange={e => setFilter(e.target.value)}>
        <option value="all">All</option>
        <option value="sales">Sales</option>
        <option value="returns">Returns</option>
      </select>
      <ExpensiveChart data={filteredData} onPointClick={handlePointClick} />
      {/* Changing filter → filteredData changes → ExpensiveChart re-renders (expected) */}
      {/* Changing other Dashboard state → memo + useCallback prevents ExpensiveChart from re-rendering */}
    </>
  );
}

// The three must work TOGETHER:
// memo alone won't help if onPointClick is a new function every render
// useCallback alone won't help if memo isn't applied to the child
// useMemo on data is needed if filteredData changes reference even when content is same

When NOT to Memoize

  • Simple value computations: `const name = `${first} ${last}`` - string concatenation is near-instant; useMemo overhead exceeds the computation cost
  • Components that always need to re-render (they receive new props every time anyway) - memo's prop comparison runs for nothing
  • Small component trees - for a form with 5 inputs, the reconciliation overhead of React itself is negligible; memoization doesn't measurably help
  • Rapidly-updating state - if a component re-renders 60 times per second intentionally (animation), memoization prevents the update you want to happen
  • The memoization itself has cost - React.memo compares every prop shallowly on every parent render; for complex prop objects this comparison can cost more than the re-render it prevents

Tip

Tip

Practice Memoization Reactmemo useMemo useCallback in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and flow for Memoization - React.memo, useMemo, useCallback

Practice Task

Note

Practice Task - (1) Write a working example of Memoization Reactmemo useMemo useCallback from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Memoization Reactmemo useMemo useCallback is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.

Key Takeaways

  • Three memoization tools, each solving a different problem: React.
  • Simple value computations: `const name = `${first} ${last}`` - string concatenation is near-instant; useMemo overhead exceeds the computation cost
  • Components that always need to re-render (they receive new props every time anyway) - memo's prop comparison runs for nothing
  • Small component trees - for a form with 5 inputs, the reconciliation overhead of React itself is negligible; memoization doesn't measurably help

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/10 done