# use-abort-signal

> 🌠 safely cancel `fetch` requests in `useEffect`

Latest version **1.0.3** (published 2024-01-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-abort-signal
pnpm add use-abort-signal
yarn add use-abort-signal
bun add use-abort-signal
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2024-01-10 |
| First published | 2023-12-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 53.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | 1adybug |
| Keywords | react, react-abort-signal, useAbortSignal, AbortSignal |

## Links

- npm: https://www.npmjs.com/package/use-abort-signal
- Repository: https://github.com/1adybug/use-abort-signal
- Homepage: https://github.com/1adybug/use-abort-signal#readme
- Issues: https://github.com/1adybug/use-abort-signal/issues
- npm.io page: https://npm.io/package/use-abort-signal

## 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.3 (latest) — 2024-01-10
- 1.0.2 — 2024-01-10
- 1.0.1 — 2024-01-05
- 1.0.0 — 2023-12-29
- 0.5.2 — 2023-12-26
- 0.5.1 — 2023-12-22
- 0.5.0 — 2023-12-22
- 0.4.4 — 2023-12-22
- 0.4.3 — 2023-12-21
- 0.4.2 — 2023-12-21
- 0.4.1 — 2023-12-21
- 0.4.0 — 2023-12-21
- 0.3.1 — 2023-12-21
- 0.3.0 — 2023-12-21
- 0.2.0 — 2023-12-21
- … 2 more at https://npm.io/package/use-abort-signal/versions

## README

# use-abort-signal

English | <a href="https://github.com/1adybug/use-abort-signal/blob/main/README.zh-CN.md">简体中文</a>

[![NPM version](https://img.shields.io/npm/v/use-abort-signal.svg?style=flat)](https://npmjs.org/package/use-abort-signal)
[![NPM downloads](https://img.shields.io/npm/dm/use-abort-signal)](https://npmjs.org/package/use-abort-signal)

During the development process with `react`, it is often necessary to make network requests within `useEffect`. However, when the dependencies of `useEffect` change or the component is unmounted, the network request might not be completed yet, leading to unexpected effects. `use-abort-signal` is designed to solve this problem by safely canceling `fetch` requests in `useEffect`.

## How to Use

### useAbortSignal

Type:

```typescript
export function useAbortSignal(effect: (signal: AbortSignal) => Promise<void>, deps?: DependencyList): void
export function useAbortSignal(effect: (signal: AbortSignal) => Promise<void>, callback: () => void, deps?: DependencyList): void
```

Usage：

```typescript
import useAbortSignal from "use-abort-signal"
// Or
import { useAbortSignal } from "use-abort-signal"

useAbortSignal(
    async signal => {
        // Do something
        // Pass the signal parameter into your fetch request
        const response = await fetch("xxx", { signal })
        // If the fetch request has not completed when the dependencies change or the component is unmounted, it will automatically cancel, and the code below will not be executed.
        // Things to do after successfully obtaining the response.
    },
    [/** dependencies */]
)
```

If you need to perform certain actions when the dependencies change or the component is unmounted, you can do so by passing the callback function as the second argument:

```typescript
import useAbortSignal from "use-abort-signal"

useAbortSignal(
    async signal => {
        // Do something
        // Pass the signal parameter into your fetch request
        const response = await fetch("xxx", { signal })
        // If the fetch request has not completed when the dependencies change or the component is unmounted, it will automatically cancel, and the code below will not be executed.
        // Things to do after successfully obtaining the response.
    },
    () => {
        // Executed when dependencies change or the component is unmounted.
    },
    [/** dependencies */]
)
```

### useAbortableFetch

Type:

```typescript
export function useAbortableFetch(effect: (fetch: typeof window.fetch) => Promise<void>, deps?: DependencyList): void
export function useAbortableFetch(effect: (fetch: typeof window.fetch) => Promise<void>, callback: () => void, deps?: DependencyList): void
```

The usage is basically the same as `useAbortSignal`, except that the first function's parameter changes from `abortSignal` to `fetch`:

```typescript
import { useAbortableFetch } from "use-abort-signal"

useAbortableFetch(
    async fetch => {
        // No need to add a signal, it will be added automatically.
        const response = await fetch("xxx")
    },
    [/** dependencies */]
)

useAbortableFetch(
    async fetch => {
        const response = await fetch("xxx")
    },
    () => {
        // Executed when dependencies change or the component is unmounted.
    },
    [/** dependencies */]
)
```

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