Course contentsShow
← React
Lesson 540 of 2,39615. useEffect FundamentalsFree lesson

Primitive Dependencies: Equality by Value

Understand how React compares primitive dependencies (strings, numbers, booleans) using strict equality.

Primitive Dependencies: Equality by Value

What you'll learn: How React determines whether primitive values in the dependency array have actually changed between renders.

The Core Idea

When you list primitive values (strings, numbers, booleans) in your useEffect dependency array, React checks if they've changed by comparing their actual values using strict equality (===). If the value is identical, React skips running the effect.

const [count, setCount] = useState(5);
const [name, setName] = useState("Alice");

useEffect(() => {
  console.log("Effect ran!");
}, [count, name]);

In this example, the effect only re-runs when count or name change to a different value. If count goes from 5 to 6, the effect runs. If it stays 5, it doesn't.

Why This Works Simply

Primitives are compared by value, not by reference. The number 5 is always equal to 5, no matter where it comes from. The string "Alice" is always equal to "Alice". React performs this check: previousCount === currentCount and previousName === currentName.

This makes primitive dependencies reliable and predictable. You don't need to worry about "different copies" of the number 5—there's only one 5.

The Analogy

Think of it like checking if someone's age changed. If their age was 25 last month and it's still 25 this month, nothing changed. The actual numeric value is what matters, not where or how you learned that number.

Key Takeaway: React compares primitive dependencies using strict equality (===), checking if the actual value has changed—making them simple, reliable dependencies in your useEffect arrays.