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

Debugging Dependency Array Issues

Techniques to identify why your memoization isn't working: logging, comparing references, and profiling.

Debugging Dependency Array Issues

You'll learn how to diagnose why your useMemo or useCallback isn't working as expected.

When memoization seems broken—your component still re-renders unnecessarily, or your expensive calculation runs every time—the problem is almost always in the dependency array. Here's how to track down the culprit:

Technique 1: Console Logging

Add logs inside your memoized function and outside to see when it runs:

const memoizedValue = useMemo(() => {
  console.log('Expensive calculation running');
  return expensiveCalculation(a, b);
}, [a, b]);

console.log('Component rendered');

If you see "Expensive calculation running" on every render, a dependency is changing unexpectedly.

Technique 2: Reference Comparison

Log dependencies before and after renders to check if references change:

const userRef = useRef();

useEffect(() => {
  if (userRef.current !== user) {
    console.log('User reference changed:', userRef.current, user);
    userRef.current = user;
  }
});

const greeting = useMemo(() => `Hello, ${user.name}`, [user]);

If user is a new object every render (even with the same values), that's your problem.

Technique 3: React DevTools Profiler

Use the Profiler to record component renders. Look for the "Why did this render?" section—it shows which props or state changed. If a memoized component renders when you expect it not to, check if a dependency prop is receiving new references.

Think of debugging like finding a water leak: you trace backward from the wet spot (the re-render) to find which pipe (dependency) is dripping.

Key Takeaway: Most memoization bugs come from unintentional reference changes—use logging, reference checks, and the Profiler to identify which dependency is unstable.