# react-use-async-loading

> Made with create-react-library

Latest version **1.1.0** (published 2026-05-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-use-async-loading
pnpm add react-use-async-loading
yarn add react-use-async-loading
bun add react-use-async-loading
```

## Health

**Score 60/100 (C)** — status: active.

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2026-05-28 |
| First published | 2022-10-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=18 |
| Dependencies | 0 |
| Unpacked size | 17.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | max_ivaneychyk |
| Keywords | react, hooks, loading, state, spinner, form, react-hooks |

## Links

- npm: https://www.npmjs.com/package/react-use-async-loading
- Homepage: https://bitbucket.org/Max123c/react-use-async-loading/src/master/README.md
- npm.io page: https://npm.io/package/react-use-async-loading

## 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.1.0 (latest) — 2026-05-28
- 1.0.2 — 2026-05-19
- 0.9.2 — 2022-10-29
- 0.9.1 — 2022-10-29
- 0.9.0 — 2022-10-29
- 0.8.1 — 2022-10-29
- 0.8.0 — 2022-10-29

## README

# react-use-async-loading

> A React hook for managing async loading state

[![NPM](https://img.shields.io/npm/v/react-use-async-loading.svg)](https://www.npmjs.com/package/react-use-async-loading) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

## Install

```bash
npm install --save react-use-async-loading
```

## Usage

```jsx
import { useLoading } from 'react-use-async-loading'
```

### Show a spinner while fetching data

```jsx
const Page = () => {
  const { loading, run } = useLoading()

  useEffect(() => {
    run(() => fetch('/data.json').then(res => res.json()).then(setData))
  }, [])

  if (loading) return <Spinner />

  return <div>Data was loaded</div>
}
```

### Disable a button while submitting

```jsx
const Form = () => {
  const { loading, run } = useLoading()

  const onSubmit = (values) => {
    run(() => axios.post('/register', values))
  }

  return <Form disabled={loading} onSubmit={onSubmit} />
}
```

### Handle error state

```jsx
const Page = () => {
  const { status, error, run } = useLoading()

  useEffect(() => {
    run(() => fetch('/data.json'))
  }, [])

  if (status === 'loading') return <Spinner />
  if (status === 'error') return <Error message={error.message} />
  if (status === 'success') return <div>Done</div>

  return null
}
```

### Cancel previous request automatically

`run()` accepts a factory function that receives an `AbortSignal`. If `run()` is called again before the previous call resolves, the previous request is aborted automatically.

```jsx
const Search = () => {
  const { loading, run } = useLoading()

  const onSearch = (query) => {
    run((signal) => fetch(`/search?q=${query}`, { signal }))
  }

  return <SearchInput loading={loading} onChange={onSearch} />
}
```

### Reset state

```jsx
const { status, run, reset } = useLoading()

// Aborts any in-flight request and resets status back to 'idle'
reset()
```

## API

```ts
const { status, loading, idle, error, run, reset } = useLoading(defaultLoading?)
```

| Name | Type | Description |
|---|---|---|
| `status` | `'idle' \| 'loading' \| 'success' \| 'error'` | Unified async state |
| `loading` | `boolean` | `true` while `run()` is pending |
| `idle` | `boolean` | `true` before `run()` has ever been called |
| `error` | `unknown` | Error from the last failed `run()`, otherwise `null` |
| `run(fn)` | `(fn: (signal) => Promise) => Promise` | Starts async operation. Aborts previous if still pending. Also accepts a plain promise. |
| `reset()` | `() => void` | Aborts in-flight request, resets all state to initial |

## License

MIT © [Max Ivaneychyk](https://bitbucket.org/Max123c/react-use-async-loading/src/master/)

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