
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
use-latest-callback
Advanced tools
React hook which returns the latest callback without changing the reference
React hook which returns the latest callback without changing the reference.
This is useful for scenarios such as event listeners where you may not want to resubscribe when the callback changes.
Open a Terminal in the project root and run:
npm install use-latest-callback
The useLatestCallback
hook accepts a function as its argument and returns a function that preserves its reference across renders.
import useLatestCallback from 'use-latest-callback';
// ...
function MyComponent() {
const callback = useLatestCallback((value) => {
console.log('Changed', value);
});
React.useEffect(() => {
someEvent.addListener(callback);
return () => someEvent.removeListener(callback);
}, [callback]);
return <>{/* whatever */}</>;
}
It's important to note that the callback is not intended to be called during the render phase. Only call the callback in response to an event.
FAQs
React hook which returns the latest callback without changing the reference
The npm package use-latest-callback receives a total of 1,230,153 weekly downloads. As such, use-latest-callback popularity was classified as popular.
We found that use-latest-callback demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.