useCallbackRef
TheuseCallbackRef hook creates a stable callback reference that doesn’t change between renders but always calls the latest version of the provided callback function. This is useful for avoiding unnecessary re-renders while ensuring your callbacks always have access to the latest props and state.
Installation
Import
Usage
Parameters
T | undefined
The callback function to create a stable reference for. Can be any function type.
Returns
T
A memoized callback function that maintains the same reference across renders but always calls the latest version of the provided callback.
Type Definition
Examples
Event Listeners
With Timers
Callback Props
Common Patterns
Avoiding Effect Dependencies
UseuseCallbackRef when you need a callback in an effect dependency array but don’t want the effect to re-run when the callback changes:
Performance Optimization
Combine withReact.memo to prevent unnecessary child re-renders: