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

Why Dependencies Care About References

How useEffect, useMemo, and useCallback dependency arrays use Object.is comparison and react to reference changes.

Why Dependencies Care About References

What you'll learn: How React's dependency arrays detect changes and why they care about object and function references, not just values.

The Comparison Mechanism

When you provide a dependency array to useEffect, useMemo, or useCallback, React doesn't peek inside your objects or arrays to see if their contents changed. Instead, it uses Object.is comparison to check if the reference itself changed.

Object.is is JavaScript's strictest equality check. For primitives like numbers and strings, it compares values. But for objects, arrays, and functions, it compares identity—are they the exact same thing in memory?

Why This Matters

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

Object.is(user, sameUser);      // true (same reference)
Object.is(user, differentUser); // false (different reference)

Even though user and differentUser have identical contents, they're different objects in memory. React sees them as different.

The Dependency Array Consequence

When React checks your dependencies on each render:

  • Primitives (5, "hello", true) compare by value—stable across renders.
  • Objects, arrays, functions compare by reference—new ones created each render are considered "changed," even if their contents are identical.

This is why inline objects in dependency arrays cause hooks to run every render, and why you learned about useMemo and useCallback to stabilize references.

Key Takeaway: React's dependency arrays use Object.is to compare references, not deep equality. New object/array/function references trigger re-runs, even with identical content—this is why referential stability matters for optimization.