# use-async-memo

> React hook for generating async memoized data.

Latest version **1.2.5** (published 2023-11-16) · MIT license · 0 weekly downloads

## Install

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

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.5 |
| Published | 2023-11-16 |
| First published | 2019-06-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 146 |
| Author | awmleer |
| Maintainers | awmleer |
| Keywords | react, hook, memo, async |

## Links

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

## 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.2.5 (latest) — 2023-11-16
- 1.2.4 — 2022-04-15
- 1.2.3 — 2021-04-25
- 1.2.2 — 2019-09-12
- 1.2.1 — 2019-09-12
- 1.2.0 — 2019-06-26
- 1.1.0 — 2019-06-26
- 1.0.0 — 2019-06-24

## README

# useAsyncMemo

React hook for generating async memoized data.

## API

```typescript
function useAsyncMemo<T>(factory: () => Promise<T>, deps: DependencyList, initial?: T): T
```

If `factory` returns `undefined` or `null`, `useAsyncMemo` will leave the memoized value **unchanged**.

## Demo

### Import

```js
import {useAsyncMemo} from "use-async-memo"
```

### Fetch API:

```js
const data = useAsyncMemo(doAPIRequest, [])
```

or

```js
const data = useAsyncMemo(() => doAPIRequest(), [])
```

or

```js
const data = useAsyncMemo(() => {
  return doAPIRequest()
}, [])
```

or

```js
const data = useAsyncMemo(async () => {
  return await doAPIRequest()
}, [])
```

### Search on inputting:

```js
const [input, setInput] = useState()
const users = useAsyncMemo(async () => {
  if (input === '') return []
  return await apiService.searchUsers(input)
}, [input], [])
```

### Get loading status:

```js
const [loading, setLoading] = useState(true)
const data = useAsyncMemo(async () => {
  setLoading(true)
  const response = await doAPIRequest()
  setLoading(false)
  return response
}, [])
```

### With ability of manual clearing:

```js
const [input, setInput] = useState()

const [clearFlag, setClearFlag] = useState({
  val: false
})
function clearItems() {
  setClearFlag({
    val: true
  })
}

const users = useAsyncMemo(async () => {
  if (clearFlag.val) {
    clearFlag.val = false
    return null
  }
  if (input === '') return []
  return await apiService.searchUsers(input)
}, [input, clearFlag], [])
```

### With debounced value:
 
> see [use-debounce](https://github.com/xnimorz/use-debounce)

```js
const [input, setInput] = useState()
const [debouncedInput] = useDebounce(input, 300)
const users = useAsyncMemo(async () => {
  if (debouncedInput === '') return []
  return await apiService.searchUsers(debouncedInput)
}, [debouncedInput], [])
```

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