Multiple Dependencies
You'll learn how to include more than one dependency in the array and understand that the effect re-runs whenever any of those values change.
The Core Idea
So far, you've seen dependency arrays with zero or one item. But most real-world effects need to respond to multiple pieces of state or props. The good news: just list them all in the array, separated by commas.
useEffect(() => {
// This effect runs when userId OR filterType changes
console.log(`Fetching data for user ${userId} with filter ${filterType}`);
}, [userId, filterType]);
React checks all dependencies before each render. If any of them changed since the last render, the effect runs again. Think of it like an OR condition: "Run this effect if dependency A changed OR dependency B changed OR dependency C changed."
How It Works
When you have [userId, filterType], React compares both values:
- If
userIdchanged butfilterTypestayed the same → effect runs - If
filterTypechanged butuserIdstayed the same → effect runs - If both changed → effect runs (once, not twice)
- If neither changed → effect skips
This makes sense: if your effect logs userId and filterType, it needs fresh output whenever either one updates.
Analogy
Imagine a notification that triggers when your email inbox OR text messages update. You don't need both to change — either one changing is enough to ping you. Multiple dependencies work the same way.
Key Takeaway: List all dependencies in the array; the effect re-runs if any of them change between renders.