# use-async-effect

> Asynchronous side effects, without the nonsense

Latest version **2.2.7** (published 2022-09-02) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.2.7 |
| Published | 2022-09-02 |
| First published | 2018-11-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 14.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 316 |
| Author | Raul de Heer |
| Maintainers | houfio, rauldeheer |
| Keywords | react, hooks, async, side-effects |

## Links

- npm: https://www.npmjs.com/package/use-async-effect
- Repository: https://github.com/rauldeheer/use-async-effect
- Issues: https://github.com/rauldeheer/use-async-effect/issues
- npm.io page: https://npm.io/package/use-async-effect

## 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

- 2.2.7 (latest) — 2022-09-02
- 2.2.6 — 2022-06-10
- 2.2.5 — 2022-01-13
- 2.2.4 — 2022-01-12
- 2.2.3 — 2021-02-26
- 2.2.2 — 2020-01-30
- 2.2.1 — 2019-09-13
- 2.2.0 — 2019-09-11
- 2.1.1 — 2019-07-19
- 2.1.0 — 2019-06-23
- 2.0.1 — 2019-03-18
- 2.0.0 — 2018-11-28
- 1.1.1 — 2018-11-27
- 1.1.0 — 2018-11-27
- 1.0.1 — 2018-11-26
- … 1 more at https://npm.io/package/use-async-effect/versions

## README

![Logo](https://raw.githubusercontent.com/rauldeheer/use-async-effect/master/logo.svg?sanitize=true)
[![npm version](https://badge.fury.io/js/use-async-effect.svg)](https://www.npmjs.com/package/use-async-effect)
[![license](https://badgen.net/npm/license/use-async-effect)](./LICENSE)

---

:running: Asynchronous side effects, without the nonsense.

```javascript
useAsyncEffect(async () => {
  await doSomethingAsync();
});
```

## Installation

```
npm install use-async-effect
```
or
```
yarn add use-async-effect
```

This package ships with TypeScript and Flow types.

## API

The API is the same as React's `useEffect()`, except for some notable differences:

- The destroy function is passed as an optional second argument:

```javascript
useAsyncEffect(callback, dependencies?);
useAsyncEffect(callback, onDestroy, dependencies?);
```

- The async callback will receive a single function to check whether the callback is still mounted:

```javascript
useAsyncEffect(async isMounted => {
  const data1 = await fn1();
  if (!isMounted()) return;

  const data2 = await fn2();
  if (!isMounted()) return;

  doSomething(data1, data2);
});
```

> Mounted means that it's running in the current component. It becomes unmounted if the component unmounts, or if the component is re-rendered and the callback is dropped and a new one is called.

## Examples

Basic mount/unmount
```javascript
useAsyncEffect(async () => console.log('mount'), () => console.log('unmount'), []);
```

Omitting destroy
```javascript
useAsyncEffect(async () => console.log('mount'), []);
```

Handle effect result in destroy
```javascript
useAsyncEffect(() => fetch('url'), (result) => console.log(result));
```

Making sure it's still mounted before updating component state
```javascript
useAsyncEffect(async isMounted => {
  const data = await fetch(`/users/${id}`).then(res => res.json());
  if (!isMounted()) return;
  setUser(data);
}, [id]);
```

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