# @vtaits/use-lazy-ref

> [![NPM](https://img.shields.io/npm/v/@vtaits/use-lazy-ref.svg)](https://www.npmjs.com/package/@vtaits/use-lazy-ref)

Latest version **0.1.4** (published 2025-01-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install @vtaits/use-lazy-ref
pnpm add @vtaits/use-lazy-ref
yarn add @vtaits/use-lazy-ref
bun add @vtaits/use-lazy-ref
```

## Health

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

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

Warnings: low downloads; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.4 |
| Published | 2025-01-10 |
| First published | 2022-09-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Vadim Taits |
| Maintainers | vtaits |
| Keywords | react, useref, callback, lazy, use-lazy-ref |

## Links

- npm: https://www.npmjs.com/package/@vtaits/use-lazy-ref
- Repository: https://github.com/vtaits/use-lazy-ref
- Issues: https://github.com/vtaits/use-lazy-ref/issues
- npm.io page: https://npm.io/package/@vtaits/use-lazy-ref

## 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

- 0.1.4 (latest) — 2025-01-10
- 0.1.3 — 2024-04-10
- 0.1.1 — 2024-03-28
- 0.1.0 — 2022-09-24

## README

[![NPM](https://img.shields.io/npm/v/@vtaits/use-lazy-ref.svg)](https://www.npmjs.com/package/@vtaits/use-lazy-ref)

# @vtaits/use-lazy-ref

`React.useRef` with initialization callback

## Example

### Good

```typescript
import { useLazyRef } from '@vtaits/use-lazy-ref';

const ref = useLazyRef(() => getRefInitialValue());
```

1. No extra boilerplate.
2. Inferred type of ref.
3. Typescript knows that a value of ref cannot be `undefined` (if it's not a possible result of `getRefInitialValue`).

### Bad

```typescript
import { useRef } from 'react';

const ref = useRef<ValueType>();

if (!ref.current) {
  ref.current = getRefInitialValue();
}
```

1. Extra boilerplate.
2. Ref type must be set.
3. Typescript thinks value of ref can be `undefined`.

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