3
3
[ ![ NPM] ( https://img.shields.io/npm/dm/react-async-hook.svg )] ( https://www.npmjs.com/package/react-async-hook )
4
4
[ ![ Build Status] ( https://travis-ci.com/slorber/react-async-hook.svg?branch=master )] ( https://travis-ci.com/slorber/react-async-hook )
5
5
6
- This library only ** does one small thing** , and ** does it well** .
6
+ This library only ** does one small thing** , and ** does it well** .
7
7
8
8
Don't expect it to grow in size, because it is ** feature complete** :
9
9
@@ -30,22 +30,18 @@ Don't expect it to grow in size, because it is **feature complete**:
30
30
31
31
** react-async-hook** :
32
32
33
-
34
- - ![ ] ( https://img.shields.io/bundlephobia/min/react-async-hook.svg )
35
- - ![ ] ( https://img.shields.io/bundlephobia/minzip/react-async-hook.svg )
36
-
33
+ - ![ ] ( https://img.shields.io/bundlephobia/min/react-async-hook.svg )
34
+ - ![ ] ( https://img.shields.io/bundlephobia/minzip/react-async-hook.svg )
37
35
38
36
** React-Query** :
39
37
40
- - ![ ] ( https://img.shields.io/bundlephobia/min/react-query.svg )
41
- - ![ ] ( https://img.shields.io/bundlephobia/minzip/react-query.svg )
42
-
38
+ - ![ ] ( https://img.shields.io/bundlephobia/min/react-query.svg )
39
+ - ![ ] ( https://img.shields.io/bundlephobia/minzip/react-query.svg )
43
40
44
41
** SWR** :
45
42
46
- - ![ ] ( https://img.shields.io/bundlephobia/min/swr.svg )
47
- - ![ ] ( https://img.shields.io/bundlephobia/minzip/swr.svg )
48
-
43
+ - ![ ] ( https://img.shields.io/bundlephobia/min/swr.svg )
44
+ - ![ ] ( https://img.shields.io/bundlephobia/minzip/swr.svg )
49
45
50
46
## Things we don't support (by design):
51
47
@@ -63,7 +59,6 @@ You can indeed build on top of this little lib to provide more advanced features
63
59
64
60
If you prefer a full-featured fetching library, try [ SWR] ( https://github.com/vercel/swr ) or [ React-Query] ( https://github.com/tannerlinsley/react-query ) .
65
61
66
-
67
62
## Usecase: loading async data into a component
68
63
69
64
The ability to inject remote/async data into a React component is a very common React need. Later we might support Suspense as well.
@@ -72,7 +67,7 @@ The ability to inject remote/async data into a React component is a very common
72
67
import { useAsync } from ' react-async-hook' ;
73
68
74
69
const fetchStarwarsHero = async id =>
75
- (await fetch (` https://swapi.co /api/people/${id }/ ` )).json ();
70
+ (await fetch (` https://swapi.dev /api/people/${id }/ ` )).json ();
76
71
77
72
const StarwarsHero = ({ id }) => {
78
73
const asyncHero = useAsync (fetchStarwarsHero , [id ]);
@@ -147,11 +142,10 @@ module.exports = {
147
142
additionalHooks: ' (useAsync|useAsyncCallback)' ,
148
143
},
149
144
],
150
- }
151
- }
145
+ },
146
+ };
152
147
```
153
148
154
-
155
149
# FAQ
156
150
157
151
#### How can I debounce the request
@@ -187,7 +181,7 @@ const searchStarwarsHero = async (
187
181
abortSignal ? : AbortSignal
188
182
): Promise <StarwarsHero []> => {
189
183
const result = await fetch (
190
- ` https://swapi.co /api/people/?search=${encodeURIComponent (text )} ` ,
184
+ ` https://swapi.dev /api/people/?search=${encodeURIComponent (text )} ` ,
191
185
{
192
186
signal: abortSignal ,
193
187
}
@@ -270,7 +264,7 @@ It is your responsability to wire the abort signal appropriately.
270
264
const StarwarsHero = ({ id }) => {
271
265
const asyncHero = useAsyncAbortable (
272
266
async (abortSignal , id ) => {
273
- const result = await fetch (` https://swapi.co /api/people/${id }/ ` , {
267
+ const result = await fetch (` https://swapi.dev /api/people/${id }/ ` , {
274
268
signal: abortSignal ,
275
269
});
276
270
if (result .status !== 200 ) {
@@ -322,13 +316,18 @@ In this case you'd better use a closure with no arg define in the dependency arr
322
316
Here, both ` state.a ` and ` state.b ` will trigger a refetch, despite b is not passed to the async fetch function.
323
317
324
318
``` tsx
325
- const asyncSomething = useAsync (() => fetchSomething (state .a ), [state .a ,state .b ]);
319
+ const asyncSomething = useAsync (() => fetchSomething (state .a ), [
320
+ state .a ,
321
+ state .b ,
322
+ ]);
326
323
```
327
324
328
325
Here, only ` state.a ` will trigger a refetch, despite b being passed to the async fetch function.
329
326
330
327
``` tsx
331
- const asyncSomething = useAsync (() => fetchSomething (state .a , state .b ), [state .a ]);
328
+ const asyncSomething = useAsync (() => fetchSomething (state .a , state .b ), [
329
+ state .a ,
330
+ ]);
332
331
```
333
332
334
333
Note you can also use this to "build" a more complex payload. Using ` useMemo ` does not guarantee the memoized value will not be cleared, so it's better to do:
@@ -354,7 +353,6 @@ const asyncSomething = useAsyncCallback(async () => {
354
353
asyncSomething .execute ();
355
354
```
356
355
357
-
358
356
#### How to support retry?
359
357
360
358
Use a lib that simply adds retry feature to async/promises directly. Doesn't exist? Build it.
0 commit comments