# use-constant

> React hook for creating a value exactly once.

Latest version **2.0.0** (published 2025-04-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-constant
pnpm add use-constant
yarn add use-constant
bun add use-constant
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2025-04-16 |
| First published | 2019-03-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 214 |
| Maintainers | andarist |

## Links

- npm: https://www.npmjs.com/package/use-constant
- Repository: https://github.com/Andarist/use-constant
- Homepage: https://github.com/Andarist/use-constant#readme
- Issues: https://github.com/Andarist/use-constant/issues
- npm.io page: https://npm.io/package/use-constant

## Recent versions

- 2.0.0 (latest) — 2025-04-16
- 1.1.1 — 2022-08-02
- 1.1.0 — 2020-11-16
- 1.0.0 — 2019-03-18

## README

# use-constant

React hook for creating a value exactly once. `useMemo` doesn't give this guarantee unfortunately - https://reactjs.org/docs/hooks-faq.html#how-to-create-expensive-objects-lazily

### Usage
Install the package
```bash
npm install use-constant
# OR
yarn add use-constant
```

In your code
```javascript
import useConstant from 'use-constant';

const MyComponent = () => {
  // Give useConstant() a function which should be be executed exactly once and
  // return in it your constant value
  const myConstantValue = useConstant(() => 42);
  // ...
```

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