File tree Expand file tree Collapse file tree 3 files changed +19
-9
lines changed
Expand file tree Collapse file tree 3 files changed +19
-9
lines changed Original file line number Diff line number Diff line change @@ -17,13 +17,15 @@ const fn = () => new Promise((resolve) => {
1717});
1818
1919const Demo = () => {
20- const { loading , value , error } = useAsync (fn);
20+ const state = useAsync (fn);
2121
2222 return (
2323 < div>
24- {loading
25- ? < div> Loading... < / div>
26- : < div> Value: {value}< / div>
24+ {state .loading ?
25+ < div> Loading... < / div>
26+ : state .error ?
27+ < div> Error ... < / div>
28+ : < div> Value: {state .value }< / div>
2729 }
2830 < / div>
2931 );
Original file line number Diff line number Diff line change 4343 "@storybook/react" : " ^3.4.11" ,
4444 "react" : " next" ,
4545 "react-dom" : " next" ,
46- "typescript" : " ^3.1.3 " ,
46+ "typescript" : " ^3.2.2 " ,
4747 "ts-node" : " ^7.0.1" ,
4848 "ts-loader" : " 3" ,
4949 "babel-core" : " ^6.26.3" ,
Original file line number Diff line number Diff line change 11import { useState , useEffect , useCallback } from 'react' ;
22
3- export interface AsyncState < T > {
4- loading : boolean ;
5- error ?: Error | any ;
6- value ?: T ;
3+ export type AsyncState < T > =
4+ | {
5+ loading : true ;
76}
7+ | {
8+ loading : false ;
9+ error : Error ;
10+ }
11+ | {
12+ loading : false ;
13+ error ?: undefined ;
14+ value : T ;
15+ } ;
816
917const useAsync = < T > ( fn : ( ) => Promise < T > , args ?) => {
1018 const [ state , set ] = useState < AsyncState < T > > ( {
You can’t perform that action at this time.
0 commit comments