Course contentsShow
← React
Lesson 2236 of 2,39649. Performance: Identifying ProblemsFree lesson

Finding Hook Dependency Issues

Use the library to spot when useMemo/useCallback dependencies are missing or causing re-computation on every render.

Finding Hook Dependency Issues

What you'll learn: How to use why-did-you-render to catch when useMemo or useCallback dependencies are missing or causing unnecessary recalculations every render.

The Problem with Hook Dependencies

When you use useMemo or useCallback, React only recomputes the memoized value when one of the dependencies changes. But two common mistakes undermine this optimization:

  1. Missing dependencies: You forget to include a value your hook actually uses, causing stale closures or bugs.
  2. Unstable dependencies: You include a reference that changes every render (like a new object or function), making the memoization pointless—it recalculates every time anyway.

Both are silent performance killers. Your hook looks optimized but behaves like it isn't.

How why-did-you-render Helps

The library tracks when useMemo or useCallback runs and checks whether it should have run based on your dependency array. If it detects:

  • A memoized value recomputing every render despite having dependencies (sign of unstable references)
  • A hook that should have updated but didn't (though this is less common)

...it logs a warning to the console explaining exactly which dependency caused the problem.

Think of it like a spell-checker for your hooks. You might think your dependency array is correct, but why-did-you-render reads the actual runtime behavior and tells you: "This useCallback recreated because options is a new object every render."

What You'll See

The console output will highlight which props or values changed identity, causing your hook to recompute. This makes it obvious when you need to memoize an upstream value or fix a dependency that shouldn't be changing.

Key Takeaway: why-did-you-render exposes when your useMemo/useCallback hooks are defeated by missing or unstable dependencies, turning invisible performance bugs into visible, fixable warnings.