Derived Values as Dependencies
What you'll learn: Why putting computed values like arr.filter() directly in your dependency array creates infinite re-render loops.
The Core Problem
A derived value is any calculation based on props or state—like users.filter(u => u.active), price * quantity, or name.toLowerCase(). When you put these directly in your dependency array, React compares them by reference on every render.
Here's the trap: computed values create new references every single render, even if the underlying data hasn't changed.
function UserList({ users }) {
const activeUsers = users.filter(u => u.active); // New array every render!
useEffect(() => {
console.log('Active users changed');
}, [activeUsers]); // ❌ Infinite loop!
}
Every render creates a new activeUsers array reference. React sees it as "different," runs the effect, which triggers a render (if it updates state), creating another new array, and so on—infinite loop.
The Fix: Depend on Source Values
Instead of depending on the derived value, depend on what it's computed from:
useEffect(() => {
const activeUsers = users.filter(u => u.active);
console.log('Active users changed');
}, [users]); // ✅ Depend on the source
Now the effect only runs when users actually changes, and you compute activeUsers inside the effect.
The Analogy
Think of a derived value like a photocopy. Every time you hit "copy," you get a new piece of paper—even if the original document is identical. React can't tell the copies are the same content; it only sees "different paper."
Key Takeaway: Never put computed values (filtered arrays, calculations, string transformations) in dependencies—they create new references every render. Depend on the source values and compute inside the effect.