← Learning Hub
Advanced

Performance: memo, useMemo and useCallback

When a component's state changes, React re-renders it and all its children. Usually that's fast enough. When it isn't, these tools skip work:

  • React.memo(Component) - skip re-rendering a child if its props haven't changed.
  • useMemo(() => value, [deps]) - remember the result of a slow calculation until its inputs change.
  • useCallback(fn, [deps]) - keep the same function between renders, so a memo child doesn't see a "new" prop.

React is fast by default, so most apps never need these tools. They matter when a component is expensive to render (a long list, a chart) or re-renders very often (typing in a search box). The idea is simple: skip work whose result wouldn't change. But memoising has its own cost, so use the Profiler to find real slow spots before adding it.

Example
<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <div id="root"></div>

    <script src="https://unpkg.com/react@18.3.1/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@18.3.1/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/@babel/standalone@7.24.7/babel.min.js"></script>

    <script type="text/babel">
      const { useState, useMemo, useCallback, memo } = React;

      const PRODUCTS = Array.from({ length: 5000 }, (_, i) => ({ id: i, name: "Item " + i, price: (i * 37) % 2000 }));

      const Row = memo(function Row({ item, onAdd }) {
        return <li>{item.name} - ₹{item.price} <button onClick={() => onAdd(item.id)}>+</button></li>;
      });

      function App() {
        const [query, setQuery] = useState("");
        const [maxPrice, setMaxPrice] = useState(500);
        const [cart, setCart] = useState(0);

        const visible = useMemo(
          () => PRODUCTS.filter(p => p.price <= maxPrice && p.name.includes(query)).slice(0, 50),
          [query, maxPrice]                          // re-filter only when these change
        );
        const add = useCallback(() => setCart(c => c + 1), []);   // same function every render

        return (
          <div>
            <p>Cart: {cart}</p>
            <input value={query} onChange={e => setQuery(e.target.value)} placeholder="Search" />
            <input type="range" min="0" max="2000" value={maxPrice} onChange={e => setMaxPrice(+e.target.value)} /> ₹{maxPrice}
            <ul>{visible.map(p => <Row key={p.id} item={p} onAdd={add} />)}</ul>
          </div>
        );
      }

      ReactDOM.createRoot(document.getElementById("root")).render(<App />);
    </script>
  </body>
</html>
▶ Try it yourself

Keys matter

Give list items a stable, unique key (an id). Using the array index as a key can mix up items when the list is sorted or filtered.

Don't add memo everywhere "just in case". Measure first with the React DevTools Profiler, then fix the slow parts.
Key points
  • React.memo skips re-rendering when props are unchanged.
  • useMemo remembers a calculated value; useCallback a function.
  • Stable keys keep list updates correct.
  • Measure first, optimise second.

📝 Test yourself

1. What does useMemo remember?
2. Why pair useCallback with React.memo children?
3. Fill in the function that skips re-rendering a child with the same props:
const Row = React.(function Row(props) { ... });