Skip to main content

useCallbackRef

The useCallbackRef 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

Use useCallbackRef 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 with React.memo to prevent unnecessary child re-renders: