Skip to main content

📘 React Mastery Series — Day 13

Published 3 min readMarkdown
📘 React Mastery Series — Day 13

useMemo vs useCallback — Stop Re-render Problems the Right Way

If you’ve ever heard:

“Your component is re-rendering unnecessarily.”

Then today’s topic is for you.


🤔 Why Do We Need useMemo & useCallback?

In React:

  • Every render recreates functions

  • Expensive calculations run again

  • Child components may re-render unnecessarily

This can affect performance in bigger apps.

React gives us:

  • useMemo → Memoize values

  • useCallback → Memoize functions

Let’s understand properly.


🧠 1️⃣ useMemo — Memoize a Value

📌 Definition

useMemo stores the result of a calculation and recomputes it only when dependencies change.


🛠 Example — Expensive Calculation

import { useState, useMemo } from "react";

function ExpensiveComponent() {
  const [count, setCount] = useState(0);
  const [text, setText] = useState("");

  const expensiveCalculation = useMemo(() => {
    console.log("Calculating...");
    return count * 1000;
  }, [count]);

  return (
    <div>
      <h2>Result: {expensiveCalculation}</h2>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <input value={text} onChange={(e) => setText(e.target.value)} />
    </div>
  );
}

🔎 What Happens?

  • Typing in input → component re-renders

  • But calculation runs ONLY when count changes

That’s optimization.


🧠 2️⃣ useCallback — Memoize a Function

📌 Definition

useCallback returns a memoized version of a function that only changes if dependencies change.


🛠 Example — Prevent Child Re-render

import { useState, useCallback } from "react";

function Parent() {
  const [count, setCount] = useState(0);

  const handleClick = useCallback(() => {
    console.log("Clicked");
  }, []);

  return (
    <div>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <Child onClick={handleClick} />
    </div>
  );
}

function Child({ onClick }) {
  console.log("Child Rendered");
  return <button onClick={onClick}>Child Button</button>;
}

Without useCallback,
Child re-renders every time Parent renders.

With useCallback,
Function reference stays same → unnecessary renders reduce.


⚡ Key Difference

useMemouseCallback
Memoizes a VALUEMemoizes a FUNCTION
Returns computed resultReturns function reference
Used for heavy calculationsUsed for preventing re-renders

🎯 When Should You Use Them?

✔ Heavy computations
✔ Large lists
✔ Passing functions to memoized children
✔ Performance-critical components


❌ When NOT to Use Them

❌ Everywhere blindly
❌ Small components
❌ Without performance issue

Overusing them can reduce readability.


🧠 Interview One-Liner

useMemo memoizes a computed value, while useCallback memoizes a function reference to prevent unnecessary re-renders.


✅ Important Rule

First write clean code.
Then optimize only if needed.

Premature optimization is not good engineering.


🔜 Coming Next

Day 14 — React.memo Explained (Stop Unnecessary Re-renders Like a Pro)


💬 If this clarified performance hooks for you, give it a ❤️
Thank you for growing consistently with me 🙌

About the author

  • Frontend Developer | React & JavaScript | Building production-ready web apps Developer with 1.5+ years of experience, currently focused on React, Redux, and full-stack development. Sharing practical tutorials, concepts, and project learnings.

No comments yet. Be the first to comment.