You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-`useAsyncEffect` now returns a cancel function directly instead of an array with it.
19
+
- Fixed bug with useAsyncEffect user cancellation; [`3ccd038`](https://github.com/DigitalBrainJS/use-async-effect/commit/3ccd03813d0b7e01132118de660f2b030967389e)
- Fixed demo links in the README.md; [`1cb5f11`](https://github.com/DigitalBrainJS/use-async-effect/commit/1cb5f11a5dc035d2755b8638b9844d726521f481)
26
+
- Added typings config to the package.json; [`481afc8`](https://github.com/DigitalBrainJS/use-async-effect/commit/481afc81612fe7c01b516cb11b9b8f084d63d3b1)
The library provides a React hook with ability to automatically cancel asynchronous code inside it.
9
-
It just makes it easier to write cancelable asynchronous code that doesn't cause
10
-
the following React issue when unmounting, if your asynchronous tasks that change state are pending:
8
+
This library provides `useAsyncEffect` and `useAsyncCallback` React hooks that make possible to cancel async code
9
+
inside it and unsubscribe the internal routines if needed (request, timers etc.).
10
+
These hooks are very useful for data fetching, since you don't have to worry about request
11
+
cancellation when components unmounts. So just forget about `isMounted` flag and `AbortController`,
12
+
which were used to protect your components from React leak warning, these hooks do that for you automatically.
13
+
Writing cancellable async code becomes easy.
14
+
## Installation :hammer:
15
+
- Install for node.js using npm/yarn:
16
+
17
+
```bash
18
+
$ npm install use-async-effect2
19
+
```
20
+
21
+
```bash
22
+
$ yarn add use-async-effect2
23
+
```
24
+
25
+
## Why
26
+
Every asynchronous procedure in your component that changes its state must properly handle the unmount event
27
+
and stop execution in some way before attempting to change the state of the unmounted component, otherwise
28
+
you will get the well-known React leakage warning:
11
29
````
12
30
Warning: Can't perform a React state update on an unmounted component.
13
31
This is an no-op, but it indicates a memory leak in your application.
14
32
To fix, cancel all subscriptions and asynchronous task in "a useEffect cleanup function".
15
33
````
34
+
16
35
It uses [c-promise2](https://www.npmjs.com/package/c-promise2) to make it work.
17
-
When it used in conjunction with other libraries from CPromise ecosystem,
36
+
When used in conjunction with other libraries from CPromise ecosystem,
18
37
such as [cp-fetch](https://www.npmjs.com/package/cp-fetch) and [cp-axios](https://www.npmjs.com/package/cp-axios),
19
38
you get a powerful tool for building asynchronous logic for your React components.
20
-
You just have to use `generators` instead of an async function to make your code cancellable,
21
-
but basically, that just means you will have to use `yield` instead of `await` keyword.
22
-
## Installation :hammer:
23
-
- Install for node.js using npm/yarn:
24
39
25
-
```bash
26
-
$ npm install use-async-effect2 c-promise2
27
-
```
40
+
`useAsyncEffect` & `useAsyncCallback` hooks make cancelable async functions from generators,
41
+
providing the ability to cancel async sequence in any stage in automatically way, when the related component unmounting.
42
+
It's totally the same as async functions, but with cancellation- you need use 'yield' instead of 'await'. That's all.
28
43
29
-
```bash
30
-
$ yarn add use-async-effect2 c-promise2
31
-
```
32
-
## Usage example
33
-
Minimal example with json request [Live demo](https://codesandbox.io/s/friendly-murdock-wxq8u?file=/src/TestComponent.js)
44
+
JSON fetching using `useAsyncEffect`[Live demo](https://codesandbox.io/s/friendly-murdock-wxq8u?file=/src/TestComponent.js)
34
45
````jsx
35
46
importReactfrom"react";
36
47
import {useState} from"react";
@@ -41,16 +52,45 @@ function JSONViewer(props) {
41
52
const [text, setText] =useState("");
42
53
43
54
useAsyncEffect(function* () {
44
-
setText("fetching...");
45
-
constresponse=yieldcpFetch(props.url);
55
+
setText("fetching...");
56
+
constresponse=yieldcpFetch(props.url);// will throw a CanceledError if component get unmounted
46
57
constjson=yieldresponse.json();
47
58
setText(`Success: ${JSON.stringify(json)}`);
48
59
}, [props.url]);
49
60
50
61
return<div>{text}</div>;
51
62
}
52
63
````
53
-
Example with a timeout & error handling ([Live demo](https://codesandbox.io/s/async-effect-demo1-vho29?file=/src/TestComponent.js)):
64
+
Notice: the related network request will be aborted, when unmounting.
65
+
66
+
It can be even easier with the [axios wrapper](https://www.npmjs.com/package/cp-axios) ([Demo](https://codesandbox.io/s/use-async-effect-axios-tiny-lsbpv?file=/src/TestComponent.js)):
This hook makes an async callback that can be automatically canceled on unmount or by user request.
227
+
-`generatorFn([scope: CPromise], ...userArguments)` : `GeneratorFunction` - generator to resolve as an async function.
228
+
Generator context (`this`) and the first argument (if `options.scopeArg` is set) refer to the CPromise instance.
229
+
165
230
#### options:
166
231
-`deps: any[]` - effect dependencies
167
232
-`combine:boolean` - subscribe to the result of the async function already
168
233
running with the same arguments or run a new one.
169
234
-`cancelPrevious:boolean` - cancel the previous pending async function before running a new one.
170
235
-`concurrency: number=0` - set concurrency limit for simultaneous calls. `0` mean unlimited.
171
236
-`queueSize: number=0` - set max queue size.
237
+
-`scopeArg: boolean=false` - pass CPromise scope to the generator function as the first argument.
172
238
173
239
## Related projects
174
240
-[c-promise2](https://www.npmjs.com/package/c-promise2) - promise with cancellation, decorators, timeouts, progress capturing, pause and user signals support
0 commit comments