The useCallback hook memoizes functions to prevent unnecessary re-renders. Here's how to use it effectively.
Basic useCallback#
Why useCallback Matters#
Dependencies Array#
useCallback with useEffect#
Passing to Custom Hooks#
Event Handlers Pattern#
useCallback vs useMemo#
Common Mistakes#
When to Use useCallback#
Performance Measurement#
Best Practices#
When to Use:
✓ Callbacks passed to memo children
✓ Callbacks in effect dependencies
✓ Callbacks in context values
✓ Expensive callback creation
Dependencies:
✓ Include all used values
✓ Use functional updates
✓ Keep deps array minimal
✓ Use refs for stable values
Performance:
✓ Profile before optimizing
✓ Combine with memo()
✓ Consider component structure
✓ Measure actual improvement
Avoid:
✗ Using without memo children
✗ Missing dependencies
✗ Overusing everywhere
✗ Premature optimization
Conclusion#
useCallback is a performance optimization that prevents function recreation between renders. Use it when passing callbacks to memoized children, when callbacks are effect dependencies, or in context values. Always include all dependencies and measure performance to ensure the optimization provides real benefits. Don't overuse it - the overhead of memoization isn't always worth it.