DEV Community

muthu raja
muthu raja

Posted on

🚨 React Re-render Methods: Reference Matters!

In React, a component re-renders only when the state reference changes.
Some array methods DO NOT create a new reference, so React won’t re-render 🚫

❌ Methods that DO NOT change reference (Mutating methods)

These modify the same array/object in memory:

push
pop
shift
unshift
sort
reverse
splice
fill
copyWithin

Enter fullscreen mode Exit fullscreen mode

⚠️ Using these directly on React state may NOT trigger a re-render

// ❌ Wrong
state.items.push(1);
setItems(state.items); // same reference

βœ… Methods that CREATE a new reference (Safe for React)

These return a new array, so React detects the change:

map
filter
concat
slice
flat
flatMap
toSorted
toReversed
toSpliced
Enter fullscreen mode Exit fullscreen mode
// βœ… Correct
setItems(prev => [...prev, 1]);
Enter fullscreen mode Exit fullscreen mode

🧠 Key Rule to Remember

React compares references, not values.
Same reference = ❌ no render
New reference = βœ… re-render

πŸ”₯ One-line takeaway

β€œMutating methods keep the same reference, so React won’t re-render. Always return a new reference when updating state.”

Buy Me a Coffee

ReactJS #JavaScript #Frontend #Immutability #WebDevelopment #ReactTips #SeniorDeveloper

Top comments (1)

Collapse
 
dariomannu profile image
Dario Mannu

In React, a component re-renders only when the state reference changes.

Hey, you forgot about when the weather changes πŸ™ˆπŸ™‰πŸ™Š and just everything re-renders