Skip to content

Commit 259d119

Browse files
committed
feat: add useAsync docs
1 parent 8a1f87b commit 259d119

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Here are some of the snippets you can use:
5656
For detailed information about each hook, check out the following links:
5757

5858
- [useFetch Documentation](https://github.com/itszeeshan/react-custom-hooks/docs/useFetch.md)
59-
- [useAsync Documentation](https://github.com/your-username/react-custom-hooks/docs/useAsync.md)
59+
- [useAsync Documentation](https://github.com/itszeeshan/react-custom-hooks/docs/useAsync.md)
6060

6161
## Contributing
6262

docs/useAsync.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
### `useAsync`
2+
3+
A hook to handle asynchronous tasks.
4+
5+
## Parameters
6+
7+
- `callback` (`function`): The asynchronous function to execute.
8+
- `dependencies` (`array`, optional): Dependency array to control when the async function should be executed.
9+
10+
## Returns
11+
12+
- An object containing `data`, `error`, and `loading`.
13+
14+
## Hooks Involved
15+
- [useState](https://react.dev/reference/react/useState)
16+
- [useCallback](https://react.dev/reference/react/useCallback)
17+
- [useEffect](https://react.dev/reference/react/useEffect)
18+
19+
## How to Use
20+
21+
```js
22+
import useAsync from "./useAsync"
23+
24+
export default function AsyncComponent() {
25+
const { loading, error, value } = useAsync(() => {
26+
return new Promise((resolve, reject) => {
27+
const success = false
28+
setTimeout(() => {
29+
success ? resolve("Hi") : reject("Error")
30+
}, 1000)
31+
})
32+
})
33+
34+
return (
35+
<div>
36+
<div>Loading: {loading.toString()}</div>
37+
<div>{error}</div>
38+
<div>{value}</div>
39+
</div>
40+
)
41+
}
42+
```

0 commit comments

Comments
 (0)