# use-async-ref

> extended react ref hook

Latest version **0.2.3** (published 2023-01-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-async-ref
pnpm add use-async-ref
yarn add use-async-ref
bun add use-async-ref
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.3 |
| Published | 2023-01-18 |
| First published | 2023-01-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 59.8 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Sina Salahshour |
| Maintainers | drhe |
| Keywords | react, useRef, useAsyncRef, typescript |

## Links

- npm: https://www.npmjs.com/package/use-async-ref
- Repository: https://github.com/Sina-Salahshour/useAsyncRef
- Homepage: https://github.com/Sina-Salahshour/useAsyncRef#readme
- Issues: https://github.com/Sina-Salahshour/useAsyncRef/issues
- npm.io page: https://npm.io/package/use-async-ref

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) 4.17.21

## 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.2.3 (latest) — 2023-01-18
- 0.2.2 — 2023-01-18
- 0.2.1 — 2023-01-18

## README

# React useAsyncRef hook

- this hook works exactly like useRef hook, but it has an additional field called `asyncCurrent`
- that returns a promise, that gets resolved when the ref gets a value other than null | undefined.
- this way, you don't need to handle states where the ref value is null, and just wait for ref to be available.

example usage:

```tsx
import { useAsyncRef } from 'use-async-ref'

const Component = () => {
  const nameRef = useAsyncRef<HTMLInputElement>(null)
  const descRef = useAsyncRef<HTMLInputElement>(null)
  const submit = async () => {
    const textCurrent = await nameRef.asyncCurrent
    const text2Current = await descRef.asyncCurrent
    console.log('name', textCurrent.value)
    console.log('desc', text2Current.value)
  }
  return (
    <div>
      <p>name</p>
      <input className="text-black" ref={nameRef} type="text" />
      <p>desc</p>
      <input className="text-black" ref={descRef} type="text" />
      <button onClick={submit}>submit</button>
    </div>
  )
}
```

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