# @utilityjs/use-lazy-initialized-value

> A React hook that holds a lazy-initialized value for a component's lifecycle.

Latest version **1.0.0** (published 2022-02-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install @utilityjs/use-lazy-initialized-value
pnpm add @utilityjs/use-lazy-initialized-value
yarn add @utilityjs/use-lazy-initialized-value
bun add @utilityjs/use-lazy-initialized-value
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-02-24 |
| First published | 2022-02-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 27 |
| Maintainers | mimshins |
| Keywords | javascript, typescript, react, react hook, lazy initialization, lazy initialization hook, lazy initialized value, lazy initialized value hook, use lazy initialized value, lazy value, lazy value hook, use lazy value |

## Links

- npm: https://www.npmjs.com/package/@utilityjs/use-lazy-initialized-value
- Repository: https://github.com/mimshins/utilityjs
- Homepage: https://github.com/mimshins/utilityjs/tree/main/src/hook/useLazyInitializedValue
- Issues: https://github.com/mimshins/utilityjs/issues
- npm.io page: https://npm.io/package/@utilityjs/use-lazy-initialized-value

## 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) — 2022-02-24

## README

<div align="center">
  <h1 align="center">
    useLazyInitializedValue
  </h1>
</div>

<div align="center">

A React hook that holds a lazy-initialized value for a component's lifecycle.

[![license](https://img.shields.io/github/license/mimshins/utilityjs?color=212121&style=for-the-badge)](https://github.com/mimshins/utilityjs/blob/main/LICENSE)
[![npm latest package](https://img.shields.io/npm/v/@utilityjs/use-lazy-initialized-value?color=212121&style=for-the-badge)](https://www.npmjs.com/package/@utilityjs/use-lazy-initialized-value)
[![npm downloads](https://img.shields.io/npm/dm/@utilityjs/use-lazy-initialized-value?color=212121&style=for-the-badge)](https://www.npmjs.com/package/@utilityjs/use-lazy-initialized-value)
[![types](https://img.shields.io/npm/types/@utilityjs/use-lazy-initialized-value?color=212121&style=for-the-badge)](https://www.npmjs.com/package/@utilityjs/use-lazy-initialized-value)

```bash
npm i @utilityjs/use-lazy-initialized-value | yarn add @utilityjs/use-lazy-initialized-value
```

</div>

<hr />

This hook calls the provided `initFactory` on mount and returns the factory value for the duration of the component's lifecycle. See React docs on [creating expensive objects lazily](https://reactjs.org/docs/hooks-faq.html#how-to-create-expensive-objects-lazily).

#### **Comparison to `useMemo`**
You may rely on `useMemo` only as a performance optimization, not as a [semantic guarantee](https://reactjs.org/docs/hooks-reference.html#usememo). 
React may throw away the cached value and recall your factory even if deps did not change.

#### **Comparison to `useState`**
You can get the same result using `useState(factory)[0]`, but it's a little more expensive supporting unused update functionality.

The right way is to implement it using `useRef` as described in React doc's [how to create expensive objects lazily](https://reactjs.org/docs/hooks-faq.html#how-to-create-expensive-objects-lazily). However, `useLazyInitializedValue` is likely more convenient and hides the `ref.current` implementation detail.

<hr />

## Usage

```tsx
const Component = () => {
  // Creating expensive object lazily
  // Can guarantee that `obj` is always same instance
  const obj = useLazyInitializedValue(() => {
    return createExpensiveObject();
  });

  // ...
}
```

## API

### `useLazyInitializedValue(initFactory)`

```ts
declare const useLazyInitializedValue: <T>(initFactory: () => T) => T;
```

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