@@ -5,7 +5,11 @@ import {
5
5
useReducer ,
6
6
useState
7
7
} from "react" ;
8
- import { CachedFetchContext , ICachedFetchOptions } from "./cachedFetchProvider" ;
8
+ import {
9
+ CachedFetchContext ,
10
+ ICachedFetchDefaultOptions ,
11
+ ICachedFetchOptions
12
+ } from "./cachedFetchProvider" ;
9
13
10
14
interface ICachedFetchReducerState {
11
15
isLoading : boolean ;
@@ -48,17 +52,39 @@ export const useCachedFetch = (
48
52
const { cache, updateCache, globalOptions } : any = useContext (
49
53
CachedFetchContext
50
54
) ;
51
- const unifiedOptions = { ...globalOptions , ...options } ;
52
- const [ headers ] = useState ( unifiedOptions . headers ) ;
55
+ const unifiedOptions : ICachedFetchDefaultOptions = {
56
+ ...globalOptions ,
57
+ ...options
58
+ } ;
59
+ const [ headers ] = useState < Headers > ( unifiedOptions . headers ) ;
53
60
const [ state , dispatch ] = useReducer ( cachedFetchReducer , {
54
61
isLoading : false ,
55
62
hasError : false
56
63
} ) ;
57
64
const [ shouldRefresh , setShouldRefresh ] = useState ( false ) ;
65
+ const [ isWaitingForDependencies , setIsWaitingForDependencies ] = useState (
66
+ true
67
+ ) ;
58
68
59
69
const fetchCallback = useCallback ( unifiedOptions . fetcher , [ ] ) ;
60
70
61
71
useEffect ( ( ) => {
72
+ if (
73
+ unifiedOptions . dependencies &&
74
+ unifiedOptions . dependencies . some ( ( dependency : boolean ) => ! dependency )
75
+ ) {
76
+ setIsWaitingForDependencies ( true ) ;
77
+ return ;
78
+ }
79
+
80
+ setIsWaitingForDependencies ( false ) ;
81
+ } , [ unifiedOptions . dependencies ] ) ;
82
+
83
+ useEffect ( ( ) => {
84
+ if ( isWaitingForDependencies ) {
85
+ return ;
86
+ }
87
+
62
88
let mounted = true ;
63
89
64
90
const fetchData = async ( ) => {
@@ -84,7 +110,15 @@ export const useCachedFetch = (
84
110
return ( ) => {
85
111
mounted = false ;
86
112
} ;
87
- } , [ route , dispatch , updateCache , fetchCallback , shouldRefresh , headers ] ) ;
113
+ } , [
114
+ route ,
115
+ dispatch ,
116
+ updateCache ,
117
+ fetchCallback ,
118
+ shouldRefresh ,
119
+ headers ,
120
+ isWaitingForDependencies
121
+ ] ) ;
88
122
89
123
const refresh = ( ) => {
90
124
setShouldRefresh ( ( current : Boolean ) => ! current ) ;
0 commit comments