๐ฃ React useCallback Hook โ Memoizing Functions in React (2025 Guide)
๐งฒ Introduction โ Why Use useCallback?
In React.js, when a component re-renders, functions defined inside it get recreated on every render. This can cause unnecessary re-renders in child componentsโespecially if those functions are passed as props.
The useCallback hook helps memoize function references, so they donโt change across renders unless their dependencies change.
๐ฏ In this guide, youโll learn:
- What
useCallbackis and how it works - When to use it (and when not to)
- Common examples like callbacks, event handlers, and child props
- Best practices for cleaner and faster React apps
๐งฉ What is the useCallback Hook?
useCallback returns a memoized version of a callback function, preventing unnecessary re-creation unless dependencies change.
โ Syntax:
const memoizedCallback = useCallback(() => {
// function body
}, [dependencies]);
๐ It’s mainly used to avoid re-passing new function references to memoized child components.
โ๏ธ 1. Why Do Functions Re-render?
Every render creates a new function identity:
const handleClick = () => console.log('clicked'); // new function every render
Even though the logic hasnโt changed, React sees a new function, which can cause child components to re-render unnecessarily.
โ 2. useCallback Example โ Prevent Unnecessary Re-renders
import { useCallback, useState } from 'react';
const Child = React.memo(({ onClick }) => {
console.log('Child re-rendered');
return <button onClick={onClick}>Click Me</button>;
});
function Parent() {
const [count, setCount] = useState(0);
const handleClick = useCallback(() => {
console.log('Clicked!');
}, []); // no re-creation unless dependencies change
return (
<>
<Child onClick={handleClick} />
<button onClick={() => setCount(c => c + 1)}>Increment</button>
</>
);
}
โ
Without useCallback, Child would re-render every time Parent does
๐งฎ 3. useCallback vs useMemo
| Hook | Returns | Use Case |
|---|---|---|
useCallback | Memoized function | Prevent re-creating functions |
useMemo | Memoized value | Prevent recalculating expensive values |
๐ Use useCallback when passing functions to memoized children or useEffect.
๐ฆ 4. useCallback for Event Handlers and API Calls
const handleSubmit = useCallback(() => {
// submit form
}, [formData]);
โ
Prevents handleSubmit from re-creating unless formData changes
โ ๏ธ 5. Common Pitfalls
| Mistake | Fix |
|---|---|
| Missing dependencies | Always include all external dependencies |
| Overusing for simple functions | Only use if identity matters (e.g., props, effects) |
Confusing with useMemo | Remember: useCallback returns functions |
๐ Best Practices
โ
Use useCallback when:
- Passing callbacks to memoized child components
- Preventing effect re-runs due to function identity
- Memoizing handlers in performance-sensitive components
โ Avoid wrapping every function in useCallback โ it’s not free (adds memory/logic overhead)
๐งฉ 6. useCallback with Custom Hooks
You can return memoized functions from custom hooks:
function useToggle(initial = false) {
const [state, setState] = useState(initial);
const toggle = useCallback(() => setState(prev => !prev), []);
return [state, toggle];
}
โ Makes your custom hooks more efficient and reusable
๐ Summary โ Recap & Next Steps
Reactโs useCallback hook helps you memoize function references to avoid unnecessary re-renders โ especially in combination with React.memo or useEffect.
๐ Key Takeaways:
useCallbackreturns a stable reference to a function- Prevents re-renders when function is passed as a prop
- Use with
React.memo()to optimize child components - Be mindful of dependencies
- Avoid over-optimization for simple cases
โ๏ธ Real-World Relevance:useCallback powers optimizations in dashboards, infinite scroll lists, data grids, and any app with frequent re-renders and complex component trees.
โ FAQ Section
โ When should I use useCallback in React?
โ
Use it when a function is passed to a memoized child or used in useEffect dependencies.
โ What is the difference between useMemo and useCallback?
โ
useMemo returns a memoized value, while useCallback returns a memoized function.
โ Does useCallback prevent all re-renders?
โ No. It prevents re-creation of functions, which helps avoid unnecessary child re-renders when used with React.memo.
โ Should I wrap every function with useCallback?
โ No. Only wrap functions that need stable references for performance-sensitive parts of your app.
โ Can I use useCallback inside useEffect?
โ
Yes. And itโs especially useful to prevent unnecessary effect re-triggers due to function identity.
Share Now :
