Skip to main content

Variable: useStableCallback

const useStableCallback: UseStableCallback

Defined in: use-stable-callback.ts:77

Returns a function with a permanent identity that always calls the latest callback.

Use it to pass a handler to a memoized child, a dependency array or a long-lived subscription (timers, listeners, observers) without re-creating or re-subscribing it on every render, and without reading stale props or state. Arguments, this and the return value are passed through, so it can wrap any function without changing its type.

callback may also be null or undefined, which is handy for optional props: the returned function then does nothing and returns undefined.

The latest callback is stored in a layout effect, so it is in place before effects and event handlers run, but it is not updated during render. Call the returned function from event handlers, effects and other callbacks, not while rendering. For reading a value rather than calling a function, see useLatest.

Type Param​

T

The type of the wrapped function.

Param​

callback

The function to call, or null/undefined for none.

Returns​

A stable function with the same signature as callback. If callback can be null or undefined, its return type also includes undefined.

Examples​

// A memoized child never re-renders because of the handler
const handleSelect = useStableCallback((id: string) => {
setSelected(id);
analytics.track(id, filter);
});

return <MemoList onSelect={handleSelect} />;
// Subscribe once, always call the latest handler
const onMessage = useStableCallback((message: string) => {
setLog((log) => [...log, `${user}: ${message}`]);
});

useEffect(() => {
const socket = connect(url);
socket.on("message", onMessage);
return () => socket.close();
}, [url, onMessage]);
// Optional prop: safe to call without `?.`
const notify = useStableCallback(props.onChange);

For a live, editable example, see the useStableCallback docs page.