# use-const-callback

> It's like `useCallback`, but you don't have to worry about dependencies.

Latest version **1.0.0** (published 2020-04-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-const-callback
pnpm add use-const-callback
yarn add use-const-callback
bun add use-const-callback
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2020-04-20 |
| First published | 2020-04-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 252.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | pie6k |
| Keywords | react, optimize, memo, performance, callback, reference, ref |

## Links

- npm: https://www.npmjs.com/package/use-const-callback
- npm.io page: https://npm.io/package/use-const-callback

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2020-04-20

## README

# use-const-callback

It's like `useCallback`, but you don't have to worry about dependencies.

## How does it work?

`useConstCallback` will return your function and will never change it's reference during entire lifecycle of the component, but when called - it will always use 'fresh' version of the callback from the last render.

eg.

```tsx
import React, { useState, useEffect, memo } from 'react';
import { useConstCallback } from 'use-const-callback';

function SomeComponent() {
  // let's re-render the component every second
  const [count, setCount] = useState(0);

  useEffect(() => {
    const interval = setInterval(() => {
      setCount((oldCount) => oldCount + 1);
    }, 1000);
  }, []);

  // we create callback that will never change it's reference, but will always
  // use last callback 'version' (so in this case it'll alert current count)
  const showCount = useConstCallback(() => {
    alert(count);
  });

  // now we use Button (which is memo component, so it'll not re-render if props doesn't change)
  // our `showCount` will never change - so this component will not re-render
  // but when called - it will show count from the last render
  return (
    <div>
      <Button onClick={showCount} />
      <div>{count}</div>
    </div>
  );
}

const Button = memo(function Memoized({ onClick }) {
  console.log('button rendered');
  return <div onClick={onClick}>Click</div>;
});
```

## Useage

```ts
useConstCallback(callbackFunction); // returns function with the same signature as callbackFunction
```

---
_Source: https://npm.io/package/use-const-callback · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
