Why useCallback Protects Function Props in React

🚀 Day 28/30 – useCallback (Stable Function References) Your child component still re-renders… even after using React.memo? 👀 Today I learned why that happens — and how to fix it ⚡ 👉 useCallback 💻 The Hidden Problem: Every render creates a new function reference ❌ That means: 👉 Parent re-renders 👉 New function gets passed as prop 👉 Child sees changed prop 👉 Child re-renders again Even with React.memo 💻 The Solution: Use useCallback ✅ It memoizes functions and keeps the same reference until dependencies change. 💻 Example: import { useState, useCallback } from "react"; const Child = React.memo(({ onClick }) => { console.log("Child Rendered"); return Click; }); function App() { const [count, setCount] = useState(0); const handleClick = useCallback(() => { console.log("Clicked"); }, []); return ( <> <button onClick={() => setCount(count + 1)}> {count} <Child onClick={handleClick} /> </> ); } 🔥 What actually happens: 1️⃣ Parent re-renders 2️⃣ handleClick keeps same reference 3️⃣ Child prop unchanged 4️⃣ React.memo skips child render ⚡ 💡 Why this matters: ✅ Better performance ✅ Fewer wasted renders ✅ Useful in optimized apps ⚡ Real Use Cases: Memoized child components Large lists with actions Dashboard widgets Event handlers passed deeply ⚡ Advanced Insight: Don’t use useCallback everywhere ❌ If no memoized child depends on it, it may add unnecessary complexity. 🔥 Key Takeaway: React.memo protects components. useCallback protects function props. Be honest 👇 Are you using useCallback smartly… or just because tutorials said so? 👀 #React #useCallback #Performance #FrontendDevelopment #JavaScript

  • text

To view or add a comment, sign in

Explore content categories