Course contentsShow
← React
Lesson 879 of 2,39623. useMemo and useCallbackFree lesson

The Object Dependency Problem

Why passing objects as dependencies often causes infinite re-memoization and how to spot it.

The Object Dependency Problem

What you'll learn: Why passing objects as dependencies in useMemo or useCallback often defeats the entire purpose of memoization.

The Core Problem

When you pass an object into a dependency array, React checks if it's changed using reference equality (===). Two objects with identical contents are not equal unless they're literally the same object in memory.

This means if your component creates a new object on every render (even with the same values inside), React thinks the dependency changed—so it re-runs your memoized code every single render.

A Common Trap

const user = { name: 'Alice', age: 30 };

const greeting = useMemo(
  () => `Hello, ${user.name}!`,
  [user] // ⚠️ Problem!
);

If user is created fresh each render, React sees a "new" object every time. Your useMemo re-calculates on every render. You've gained nothing—and actually added overhead.

How to Spot It

  • Symptom: Your memoized value or callback seems to run constantly despite "nothing changing."
  • Cause: Check if dependencies are objects created inline or passed down without memoization themselves.
  • DevTools: Use React DevTools Profiler to see if components re-render unexpectedly when you thought they were optimized.

The Chain Reaction

If you memoize based on an object that's always new, then anything depending on that memoized value also breaks. It cascades through your component tree.

Key Takeaway: Objects created on every render will break memoization. Always question whether object dependencies are stable, or if you should memoize them first—or depend on their primitive properties instead.