React Re-renders Despite Unchanged Data

REACT INTERNALS - PART 5 Why React Re-renders Even When Data Looks the Same By now we know the common triggers of re-render: • state updates • parent re-render • context changes But there is something that confuses many developers: “Why did my component re-render even when the data didn’t change?” The answer lies in reference identity. 🔥 React Uses Reference Comparison React does not deeply compare objects, arrays, or functions. Instead, it compares references. Example: const user1 = { name: "Ashwini" } const user2 = { name: "Ashwini" } console.log(user1 === user2) // false Even though the values look identical, they are different objects in memory. So React treats them as different values. ⚠️ Real World Example (Object) function Parent() { return <Child config={{ theme: "dark" }} /> } Every time Parent renders: • a new object is created • the reference changes • React assumes props changed Result → Child re-renders Even though "dark" never changed. ⚠️ Another Hidden Case (Functions) Functions also create new references on every render. function Parent() { const handleClick = () => { console.log("Clicked") } return <Child onClick={handleClick} /> } Each render creates a new function instance. So React sees: new reference → props changed → re-render 🎯 Key Insight React compares: • Objects → by reference • Arrays → by reference • Functions → by reference Not by deep equality. Two values may look identical but still be different for React. ⚡ This is one of the hidden reasons many React apps re-render more than expected. Tools like React.memo, useMemo, and useCallback help stabilize references and prevent unnecessary renders. Next: How React.memo Prevents Unnecessary Re-renders #ReactJS #FrontendDevelopment #JavaScript #ReactInternals #SoftwareEngineering

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories