use-stable-callback
TypeScript icon, indicating that this package has built-in type declarations

1.1.5 • Public • Published

useStableCallback

A substitute for useCallback that returns a memoized version of whatever callback that it is given.

The returned function maintains a stable reference, but it will always have accesss to the most recent scope of the component in which it is used.

function MyComponent({ url }: { url: string }) {
  const refreshData = useStableCallback(() =>
    fetch(url, { method: 'GET' }).then(r => r.json())
  );

  return <SomeChildComponent refresh={refreshData} />;
}

Readme

Keywords

none

Package Sidebar

Install

npm i use-stable-callback

Weekly Downloads

2

Version

1.1.5

License

ISC

Unpacked Size

3.55 kB

Total Files

5

Last publish

Collaborators

  • mariusbrataas