Socket
Socket
Sign inDemoInstall

use-latest-callback

Package Overview
Dependencies
0
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    use-latest-callback

React hook which returns the latest callback without changing the reference


Version published
Weekly downloads
755K
decreased by-0.72%
Maintainers
1
Install size
4.71 kB
Created
Weekly downloads
 

Readme

Source

use-latest-callback

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.

Installation

Open a Terminal in the project root and run:

npm install use-latest-callback

Usage

The useLatestCallback hook accepts a function as its argument, and returns a function which preserves its reference across renders.

const useLatestCallback = require('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 */}</>;
}

Keywords

FAQs

Last updated on 24 Aug 2022

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc