File tree Expand file tree Collapse file tree 6 files changed +109
-1
lines changed Expand file tree Collapse file tree 6 files changed +109
-1
lines changed Original file line number Diff line number Diff line change 31
31
## Reference
32
32
33
33
- [ __ Sensors__ ] ( ./docs/Sensors.md )
34
- - [ ` useBattery ` ] ( ./docs/useBattery.md ) &mdash ; tracks device batter state.
34
+ - [ ` useBattery ` ] ( ./docs/useBattery.md ) &mdash ; tracks device battery state.
35
35
- [ ` useGeolocation ` ] ( ./docs/useGeolocation.md ) &mdash ; tracks geo location state of user's device.
36
36
- [ ` useHover ` ] ( ./docs/useHover.md ) &mdash ; tracks mouse hover state of some element.
37
37
- [ ` useIdle ` ] ( ./docs/useIdle.md ) &mdash ; tracks whether user is being inactive.
53
53
<br />
54
54
<br />
55
55
- [ __ Side-effects__ ] ( ./docs/Side-effects.md )
56
+ - [ ` useAsync ` ] ( ./docs/useAsync.md ) &mdash ; resolves an ` async ` function.
56
57
- [ ` useCss ` ] ( ./docs/useCss.md ) &mdash ; dynamically adjusts CSS.
57
58
- [ ` useFavicon ` ] ( ./docs/useFavicon.md ) &mdash ; sets favicon of the page.
58
59
- [ ` useTitle ` ] ( ./docs/useTitle.md ) &mdash ; sets title of the page.
Original file line number Diff line number Diff line change
1
+ # ` useAsync `
2
+
3
+ React hook that resolves an ` async ` function or a function that returns
4
+ a promise;
5
+
6
+
7
+ ## Usage
8
+
9
+ ``` jsx
10
+ import {useAsync } from ' react-use' ;
11
+
12
+ // Returns a Promise that resolves after one second.
13
+ const fn = () => new Promise ((resolve ) => {
14
+ setTimeout (() => {
15
+ resolve (' RESOLVED' );
16
+ }, 1000 );
17
+ });
18
+
19
+ const Demo = () => {
20
+ const {loading , value , error } = useAsync (fn);
21
+
22
+ return (
23
+ < div>
24
+ {loading
25
+ ? < div> Loading... < / div>
26
+ : < div> Value: {value}< / div>
27
+ }
28
+ < / div>
29
+ );
30
+ };
31
+ ```
Original file line number Diff line number Diff line change
1
+ import * as React from 'react' ;
2
+ import { storiesOf } from '@storybook/react' ;
3
+ import { useAsync } from '..' ;
4
+
5
+ const fn = ( ) => new Promise ( ( resolve ) => {
6
+ setTimeout ( ( ) => {
7
+ resolve ( 'RESOLVED' ) ;
8
+ } , 1000 ) ;
9
+ } ) ;
10
+
11
+ const Demo = ( ) => {
12
+ const { loading, value} = useAsync ( fn ) ;
13
+
14
+ return (
15
+ < div >
16
+ { loading
17
+ ? < div > Loading...</ div >
18
+ : < div > Value: { value } </ div >
19
+ }
20
+ </ div >
21
+ ) ;
22
+ } ;
23
+
24
+ storiesOf ( 'useAsync' , module )
25
+ . add ( 'Example' , ( ) =>
26
+ < Demo />
27
+ )
Original file line number Diff line number Diff line change
1
+ import useAsync from './useAsync' ;
1
2
import useBattery from './useBattery' ;
2
3
import useCounter from './useCounter' ;
3
4
import useCss from './useCss' ;
@@ -23,6 +24,7 @@ import useTween from './useTween';
23
24
import useWindowSize from './useWindowSize' ;
24
25
25
26
export {
27
+ useAsync ,
26
28
useBattery ,
27
29
useCounter ,
28
30
useCss ,
Original file line number Diff line number Diff line change @@ -8,3 +8,6 @@ export const useEffect: UseEffect = (React as any).useEffect;
8
8
9
9
export type UseRef = < T > ( initialValue : T ) => { current : T } ;
10
10
export const useRef : UseRef = ( React as any ) . useRef ;
11
+
12
+ export type UseCallback = < T extends ( ( ...args : any [ ] ) => any ) > ( callback : T , args : any [ ] ) => T ;
13
+ export const useCallback : UseCallback = ( React as any ) . useCallback ;
Original file line number Diff line number Diff line change
1
+ import { useState , useEffect , useCallback } from './react' ;
2
+
3
+ export interface AsyncState < T > {
4
+ loading : boolean ;
5
+ error ?: Error | any ;
6
+ value ?: T ;
7
+ }
8
+
9
+ const useAsync = < T > ( fn : ( ) => Promise < T > , args ?) => {
10
+ const [ state , set ] = useState < AsyncState < T > > ( {
11
+ loading : true ,
12
+ } ) ;
13
+ const memoized = useCallback ( fn , args ) ;
14
+
15
+ useEffect ( ( ) => {
16
+ let mounted = true ;
17
+ const promise = memoized ( ) ;
18
+
19
+ promise
20
+ . then ( value => {
21
+ if ( mounted ) {
22
+ set ( {
23
+ loading : false ,
24
+ value,
25
+ } ) ;
26
+ }
27
+ } , error => {
28
+ if ( mounted ) {
29
+ set ( {
30
+ loading : false ,
31
+ error,
32
+ } ) ;
33
+ }
34
+ } ) ;
35
+
36
+ return ( ) => {
37
+ mounted = false ;
38
+ } ;
39
+ } , [ memoized ] ) ;
40
+
41
+ return state ;
42
+ } ;
43
+
44
+ export default useAsync ;
You can’t perform that action at this time.
0 commit comments