You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Mathilde JEAN db2c45d692
coucou toto petit push cadeau
2 years ago
..
lib coucou toto petit push cadeau 2 years ago
src coucou toto petit push cadeau 2 years ago
README.md coucou toto petit push cadeau 2 years ago
package.json coucou toto petit push cadeau 2 years ago

README.md

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 */}</>;
}