File tree 5 files changed +163
-5
lines changed
5 files changed +163
-5
lines changed Original file line number Diff line number Diff line change 18
18
- Written in typescript to achieve good type support.
19
19
- Constantly add new and useful hooks.
20
20
21
- ## 🌈 Install
21
+ ## 🌈 Installation
22
22
23
23
```
24
24
$ npm install @mints/hooks
@@ -32,7 +32,11 @@ import { useRequest } from '@mints/hooks';
32
32
33
33
## 📃 Hook List
34
34
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 )
Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change
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
+ ```
You can’t perform that action at this time.
0 commit comments