
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
use-callback-stable
Advanced tools
A friendlier, faster, and safer alternative to useCallback. It returns a stable function reference without needing a dependency array.
npm install use-callback-stable
yarn add use-callback-stable
import React, { useState } from 'react';
import { useCallbackStable } from 'use-callback-stable';
const MyComponent = () => {
const [count, setCount] = useState(0);
const incrementState = useCallbackStable(() => {
// You will always have the current value of variables.
setCount(count + 1);
});
return (
<div>
<p>{count}</p>
<button onClick={incrementState}>Increment State</button>
</div>
);
};
With useCallbackStable, you can be confident that your callback always has access to the latest state and props, and you won’t need to worry about dependency arrays or stale closures.
In JavaScript, as long as a function is in memory, all the variables in its containing closure are also retained. This means that using useCallback and useEffect can inadvertently create memory leaks because they hold onto the entire state of your component at the time they were created. This problem can be compounded by memoized data and other callbacks.
By using useCallbackStable, you avoid retaining old closures, as it always references the latest version of your callback function. This leads to improved memory usage and prevents potential memory leaks.
For further details, see this article: React Closures and Memory Leaks.
Because useCallback returns a new function reference whenever its dependency array changes, child components may re-render unnecessarily or trigger unwanted side effects.
With useCallbackStable, you get a single function reference that remains stable throughout the life of your component. It will always call the most recent version of your function, using your variables in their latest state. This helps prevent unnecessary re-renders and side effects in child components that depend on the callback.
import { useCallback, useRef } from 'react';
function useCallbackStable<T extends any[], U>(callback: (...args: T) => U): (...args: T) => U {
const callbackRef = useRef(callback);
// Update the ref to the latest callback on each render
callbackRef.current = callback;
// Return a stable callback function
return useCallback((...args: T) => {
return callbackRef.current(...args);
}, []);
}
FAQs
A faster, friendlier, and safer useCallback
The npm package use-callback-stable receives a total of 2,637 weekly downloads. As such, use-callback-stable popularity was classified as popular.
We found that use-callback-stable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.