-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a1f87b
commit 259d119
Showing
2 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
### `useAsync` | ||
|
||
A hook to handle asynchronous tasks. | ||
|
||
## Parameters | ||
|
||
- `callback` (`function`): The asynchronous function to execute. | ||
- `dependencies` (`array`, optional): Dependency array to control when the async function should be executed. | ||
|
||
## Returns | ||
|
||
- An object containing `data`, `error`, and `loading`. | ||
|
||
## Hooks Involved | ||
- [useState](https://react.dev/reference/react/useState) | ||
- [useCallback](https://react.dev/reference/react/useCallback) | ||
- [useEffect](https://react.dev/reference/react/useEffect) | ||
|
||
## How to Use | ||
|
||
```js | ||
import useAsync from "./useAsync" | ||
|
||
export default function AsyncComponent() { | ||
const { loading, error, value } = useAsync(() => { | ||
return new Promise((resolve, reject) => { | ||
const success = false | ||
setTimeout(() => { | ||
success ? resolve("Hi") : reject("Error") | ||
}, 1000) | ||
}) | ||
}) | ||
|
||
return ( | ||
<div> | ||
<div>Loading: {loading.toString()}</div> | ||
<div>{error}</div> | ||
<div>{value}</div> | ||
</div> | ||
) | ||
} | ||
``` |