Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nutgaard/use-async
useAsync hook for use with react
https://github.com/nutgaard/use-async
Last synced: 2 months ago
JSON representation
useAsync hook for use with react
- Host: GitHub
- URL: https://github.com/nutgaard/use-async
- Owner: nutgaard
- License: mit
- Created: 2019-07-21T10:57:15.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T01:31:35.000Z (about 2 years ago)
- Last Synced: 2024-11-08T01:17:46.118Z (2 months ago)
- Language: TypeScript
- Size: 2.57 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 23
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# use-async
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![Travis](https://img.shields.io/travis/nutgaard/use-async.svg)](https://travis-ci.org/nutgaard/use-async)
[![codecov](https://codecov.io/gh/nutgaard/use-async/branch/master/graph/badge.svg)](https://codecov.io/gh/nutgaard/use-async)
[![dependencies Status](https://david-dm.org/nutgaard/use-async/status.svg)](https://david-dm.org/nutgaard/use-async)### Installation
```
npm install @nutgaard/use-async --save
```### Usage
The library exposes one hook `useAsync`, and three utility-functions to help use the result (`isPending`, `hasData` and `hasError`).```typescript
import React from 'react';
import useAsync, { isPending, hasError } from '@nutgaard/use-async';const source = React.useCallback((isRerun) => Promise.resolve("your data here"), []);
const result = useAsync(source);if (isPending(result)) {
return ;
} else if (hasError(result)) {
return
}return
{result.data}
```### useAsync API
| Argument | Type | Optional | Default |
| ------------- | ------------- | ------------- | ------------- |
| `source` | `(isRerun) => Promise` | No | - |
| `lazy` | `boolean` | Yes | `false` |
| `dependencyList` | `Array` | Yes | `undefiend` |In cases where `dependencyList` is defined it is passed on to `useEffect` instead of `source`.
This allows a greater control of when the effect should run in cases where the `source` does
not necessarily change.#### Types
Full documentation of types can be seen [here](https://www.utgaard.xyz/use-async/), or in the 80-ish lines of code.## Credits
Made using the awesome [typescript library starter](https://github.com/alexjoverm/typescript-library-starter)