React Compares Old and New State
You'll learn: How React decides whether your state update actually requires a re-render.
The Comparison Check
When you call setState, React doesn't blindly re-render your component every time. Instead, it compares the new state value with the old one to see if anything actually changed. If the values are identical, React skips the re-render entirely—there's no point in updating the UI if nothing changed!
React uses a comparison method called Object.is() to check equality. This is almost identical to using === (strict equality), with a couple of edge-case differences.
What This Means for Your Code
const [count, setCount] = useState(0);
// Clicking this WILL cause a re-render (0 → 1)
<button onClick={() => setCount(1)}>Set to 1</button>
// Clicking this again does NOTHING (1 → 1)
// React sees they're the same and skips re-rendering
<button onClick={() => setCount(1)}>Set to 1 again</button>
Think of React as a smart bouncer at a club: before letting anyone in (re-rendering), it checks if the new guest is actually different from who's already inside. If it's the same person trying to re-enter, the bouncer waves them off—no need to process the same thing twice.
This optimization is automatic—you don't need to do anything special. React handles it behind the scenes every time you call a state setter.
Key Takeaway: React uses Object.is() to compare old and new state values—if they're identical, the re-render is skipped automatically as a performance optimization.