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

Dependency Array Rules Apply to Memoization Hooks

Understanding how useMemo and useCallback follow the same dependency array principles as useEffect.

Dependency Array Rules Apply to Memoization Hooks

You'll learn that useMemo and useCallback follow the exact same dependency array rules as useEffect.

The Same Rules, Different Purpose

Remember learning about dependency arrays in useEffect? Every rule you learned applies identically to useMemo and useCallback. These hooks share the same underlying mechanism for tracking when to re-run their logic.

The core rules refresher:

  • List every value from outside the hook that's used inside
  • Primitive values trigger re-run when they change identity
  • Object/array references trigger re-run when the reference changes
  • React does shallow comparison (using Object.is)
  • Missing dependencies cause stale closures
  • Empty array [] means "never re-compute"
  • No array means "re-compute every render"

Why This Matters for Memoization

When you write useMemo(() => expensiveCalculation(a, b), [a, b]), React watches those dependencies. If a or b change between renders, the calculation runs again. If they don't change, React returns the cached result.

Think of it like a recipe card with a version number. You only cook the meal again when the ingredients (dependencies) change. The recipe card (your memoization hook) checks the ingredient versions before deciding whether to use leftovers or cook fresh.

The same applies to useCallback: it checks whether the dependencies changed before deciding to create a new function or return the cached one.

Common Mistakes Carry Over

If you accidentally omit user.id from your dependency array, your memoized value will become stale — just like with useEffect. React's linter will warn you about missing dependencies in all three hooks.

Key Takeaway: useMemo and useCallback aren't special cases — they use the exact same dependency tracking system as useEffect, so every rule about dependency arrays you've learned applies equally to memoization.