Skip to content

Commit 7c4b1e3

Browse files
committed
feat: add docs for all hooks
1 parent 09214e2 commit 7c4b1e3

5 files changed

+163
-5
lines changed

README.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
- Written in typescript to achieve good type support.
1919
- Constantly add new and useful hooks.
2020

21-
## 🌈 Install
21+
## 🌈 Installation
2222

2323
```
2424
$ npm install @mints/hooks
@@ -32,7 +32,11 @@ import { useRequest } from '@mints/hooks';
3232

3333
## 📃 Hook List
3434

35-
- useRequest
36-
- useAutoRefresh
37-
- useOutsideClick
38-
- useDebounce
35+
- [useRequest](./docs/use-request.md)
36+
- [useAutoRefresh](./docs/use-auto-refresh.md)
37+
- [useOutsideClick](./docs/use-outside-click.md)
38+
- [useDebounce](./docs/use-debounce.md)
39+
40+
## 🌐 LICENSE
41+
42+
[MIT](./LICENSE)

docs/use-auto-refresh.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# useAutoRefresh
2+
3+
> A hook for automatic refresh.
4+
5+
## Usage
6+
7+
```javascript
8+
import { useAutoRefresh } from '@mints/hooks';
9+
10+
const Example = () => {
11+
const { loading, data } = useAutoRefresh(
12+
async () => {
13+
const res = await fetch(url).json();
14+
return res;
15+
},
16+
{
17+
retryLimit: 5,
18+
},
19+
);
20+
21+
if (loading || !data) {
22+
return <div>Loading...</div>;
23+
}
24+
25+
if (stoped) {
26+
return <div>Automatic Stoped...</div>;
27+
}
28+
29+
return <div>{JSON.stringfiy(data)}</div>;
30+
};
31+
```
32+
33+
## API
34+
35+
```typescript
36+
useAutoRefresh = <T>(
37+
request: (signal: AbortSignal) => Promise<T>,
38+
option?: {
39+
stop?: (data?: T) => boolean;
40+
interval?: number;
41+
retryLimit?: number;
42+
},
43+
): {
44+
loading: boolean;
45+
data?: T;
46+
error?: unknow;
47+
stoped: boolean;
48+
}
49+
```

docs/use-debounce.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# useDebounce
2+
3+
> A hook that deal with the debounced value.
4+
5+
## Usage
6+
7+
```javascript
8+
import { useState } from 'react';
9+
import { useDebounce } from '@mints/hooks';
10+
11+
const Example = () => {
12+
const [value, setValue] = useState('');
13+
const debouncedValue = useDebounce(value, { wait: 500 });
14+
15+
return (
16+
<div>
17+
<input value={value} onChange={(e) => setValue(e.target.value)} />
18+
<span>DebouncedValue: {debouncedValue}</span>
19+
</div>
20+
);
21+
};
22+
```
23+
24+
## API
25+
26+
```typescript
27+
useDebounce = <T>(
28+
value: T,
29+
options?: {
30+
wait?: number;
31+
leading?: boolean;
32+
maxWait?: number;
33+
trailing?: boolean;
34+
},
35+
): T
36+
```

docs/use-outside-click.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# useOutsideClick
2+
3+
> A hook that helps you bind click events outside of a specified element.
4+
5+
## Usage
6+
7+
```javascript
8+
import { useRef } from 'react';
9+
import { useOutsideClick } from '@mints/hooks';
10+
11+
const Example = () => {
12+
const [open, setOpen] = useState(false);
13+
14+
const ref = useRef(null);
15+
16+
useOutsideClick(ref, () => setOpen(false));
17+
18+
return (
19+
<div ref={ref}>
20+
<span onClick={() => setOpen(true)}>Control</span>
21+
{open && <div>Show Something.</div>}
22+
</div>
23+
);
24+
};
25+
```
26+
27+
## API
28+
29+
```typescript
30+
useOutsideClick = <T>(
31+
ref: MutableRefObject<HTMLDivElement | null>,
32+
cb: () => void,
33+
)
34+
```

docs/use-request.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# useRequest
2+
3+
> A hook for asynchronous requests.
4+
5+
## Usage
6+
7+
```javascript
8+
import { useRequest } from '@mints/hooks';
9+
10+
const Example = () => {
11+
const { loading, data } = useRequest(async () => {
12+
const res = await fetch(url).json();
13+
return res;
14+
}, []);
15+
16+
if (loading || !data) {
17+
return <div>Loading...</div>;
18+
}
19+
20+
return <div>{JSON.stringfiy(data)}</div>;
21+
};
22+
```
23+
24+
## API
25+
26+
```typescript
27+
useRequest = <T>(
28+
request: (signal: AbortSignal) => Promise<T>,
29+
deps: React.DependencyList = [],
30+
): {
31+
loading: boolean;
32+
data?: T;
33+
error?: unknow;
34+
}
35+
```

0 commit comments

Comments
 (0)