Primitive vs Reference Dependencies
What you'll learn: Why useMemo and useCallback behave differently with numbers/strings versus objects/arrays/functions in their dependency arrays.
The Comparison Problem
When React checks if dependencies have changed, it uses Object.is() to compare the old and new values. This comparison works differently for primitives (numbers, strings, booleans) versus references (objects, arrays, functions).
Primitives compare by value:
Object.is(5, 5) // true
Object.is("hello", "hello") // true
References compare by identity:
Object.is({}, {}) // false — different objects!
Object.is([1, 2], [1, 2]) // false — different arrays!
Even if two objects contain identical data, they're different references in memory.
Why This Matters for Dependencies
const count = 5;
useMemo(() => expensiveCalc(count), [count]); // ✅ Works as expected
const config = { threshold: 5 };
useMemo(() => expensiveCalc(config), [config]); // ⚠️ Runs every render!
The count primitive is stable between renders if the value doesn't change. But config is a new object reference on every render, even with the same properties, so the memoization breaks.
The Mental Model
Think of primitives like comparing the contents of two identical letters — if the words match, they're the same.
Think of references like comparing memory addresses — two houses can look identical, but they're different buildings at different locations.
Key Takeaway: Primitive dependencies (numbers, strings, booleans) are stable across renders if their values don't change. Object, array, and function dependencies need their own memoization (via useMemo/useCallback) to stay stable, or they'll break your memoization every render.