React Re-renders on State Setter Calls, Not Value Changes

🧠 React Doesn’t Re-render Because State Changed We’ve all heard this: “React re-renders when state changes.” Sounds correct. But it’s not the full truth. 🔍 What actually triggers a re-render? React re-renders when: 👉 You call a state setter (setState) Not when the value “changes”. Example const [count, setCount] = useState(0); setCount(0); Even though the value is the same… 👉 React may still schedule a render. Why? Because React doesn’t first check: “Did the value change?” It reacts to: “Did you ask for an update?” 🧠 So what happens next? React does a quick comparison (Object.is) If the value is the same: 👉 It bails out and skips unnecessary DOM updates But the render phase can still happen. 🎯 Why this matters This explains why: Your component “renders” but UI doesn’t change Memoization sometimes feels inconsistent Performance issues appear in unexpected places 💥 The Real Mental Model React doesn’t track changes. It responds to update requests. 🧠 Senior Insight Optimizing React is not about “reducing state changes” It’s about 👉 reducing unnecessary updates #ReactJS #FrontendDevelopment #JavaScript #ReactInternals #WebPerformance #CleanCode #LearningInPublic

To view or add a comment, sign in

Explore content categories