mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
35 lines
628 B
Markdown
35 lines
628 B
Markdown
# `useAsync`
|
|
|
|
React hook that resolves an `async` function or a function that returns
|
|
a promise;
|
|
|
|
## Usage
|
|
|
|
```jsx
|
|
import {useAsync} from 'react-use';
|
|
|
|
const Demo = ({delay = 1000}) => {
|
|
const state = useAsync(() => {
|
|
// Returns a Promise that resolves after x milliseconds
|
|
return new Promise((resolve) => setTimeout(() => resolve('RESOLVED'), delay);
|
|
}, [delay]);
|
|
|
|
return (
|
|
<div>
|
|
{state.loading?
|
|
<div>Loading...</div>
|
|
: state.error?
|
|
<div>Error...</div>
|
|
: <div>Value: {state.value}</div>
|
|
}
|
|
</div>
|
|
);
|
|
};
|
|
```
|
|
|
|
## Reference
|
|
|
|
```ts
|
|
useAsync(fn, args?: any[]);
|
|
```
|