How useCallback() saved my React project from unnecessary re-renders

⚛️ That moment when I finally understood useCallback() 😅 While working on a React project, I had a parent component that passed a function down to a child via props. Everything looked fine — until I noticed my child component was re-rendering every time I clicked anything, even if the data didn’t change! 🤯 After a few rounds of confusion (and console logs everywhere 😆), I discovered the culprit: React was re-creating the function on every render. That’s when I met my new best friend — useCallback() 💪 Here’s how it saved me 👇 ❌ Before: function Parent() { const [count, setCount] = useState(0); const handleClick = () => console.log("Clicked!"); return <Child onClick={handleClick} />; } ✅ After using useCallback(): function Parent() { const [count, setCount] = useState(0); const handleClick = useCallback(() => console.log("Clicked!"), []); return <Child onClick={handleClick} />; } 💡 Lesson learned: useCallback() tells React: 👉 “Hey, this function is the same unless dependencies change.” No more unnecessary re-renders. 🚀 React isn’t just about writing components — it’s about learning how to make them efficient! #ReactJS #useCallback #WebDevelopment #MERNStack #FrontendDeveloper #PerformanceOptimization #LearningByDoing #JavaScript #ReactHooks

Bogdan Ioanas

Senior Software Engineer. Pushing pixels React Native way!

5mo

Nice ChatGPT post. But did you really understand what is really happening? 🫣

To view or add a comment, sign in

Explore content categories