@@ -51,15 +51,17 @@ const useDataLoader = (
51
51
pollingInterval,
52
52
} = { } ,
53
53
) => {
54
- const { addCachedData , addReload, clearReload, getCachedData } =
54
+ const { addReload, clearReload, getCachedData, addCachedData } =
55
55
useDataLoaderContext ( )
56
56
const [ { status, error } , dispatch ] = useReducer ( reducer , {
57
57
error : undefined ,
58
58
status : StatusEnum . IDLE ,
59
59
} )
60
60
61
+ const addReloadRef = useRef ( addReload )
62
+ const clearReloadRef = useRef ( clearReload )
61
63
const previousDataRef = useRef ( )
62
- const keyRef = useRef ( key )
64
+ const isMountedRef = useRef ( enabled )
63
65
const methodRef = useRef ( method )
64
66
const onSuccessRef = useRef ( onSuccess )
65
67
const onErrorRef = useRef ( onError )
@@ -96,53 +98,46 @@ const useDataLoader = (
96
98
97
99
useEffect ( ( ) => {
98
100
let handler
99
- if ( enabled ) {
100
- if ( ! isIdle && keyRef . current !== key ) {
101
- keyRef . current = key
102
- dispatch ( Actions . createReset ( ) )
103
- } else {
104
- if ( isIdle ) {
105
- keyRef . current = key
106
- handleRequest . current ( key )
107
- }
108
- if ( pollingInterval && isSuccess && ! handler ) {
109
- handler = setTimeout (
110
- ( ) => handleRequest . current ( key ) ,
111
- pollingInterval ,
112
- )
113
- }
114
- if ( key && typeof key === 'string' ) {
115
- addReload ( key , reloadArgs => handleRequest . current ( key , reloadArgs ) )
116
- }
101
+ if ( isMountedRef . current ) {
102
+ if ( isIdle ) {
103
+ handleRequest . current ( key )
104
+ }
105
+ if ( pollingInterval && isSuccess ) {
106
+ handler = setTimeout ( ( ) => handleRequest . current ( key ) , pollingInterval )
117
107
}
118
108
}
119
109
120
110
return ( ) => {
121
- if ( key && typeof key === 'string' ) {
122
- clearReload ( key )
123
- }
124
- if ( handler ) {
125
- clearTimeout ( handler )
126
- handler = undefined
127
- }
111
+ if ( handler ) clearTimeout ( handler )
128
112
}
129
113
// Why can't put empty array for componentDidMount, componentWillUnmount ??? No array act like componentDidMount and componentDidUpdate
130
- } , [
131
- enabled ,
132
- key ,
133
- clearReload ,
134
- addReload ,
135
- addCachedData ,
136
- getCachedData ,
137
- pollingInterval ,
138
- isIdle ,
139
- isSuccess ,
140
- ] )
114
+ } , [ key , pollingInterval , isIdle , isSuccess ] )
141
115
142
116
useLayoutEffect ( ( ) => {
143
117
methodRef . current = method
144
118
} , [ method ] )
145
119
120
+ useLayoutEffect ( ( ) => {
121
+ isMountedRef . current = enabled
122
+ dispatch ( Actions . createReset ( ) )
123
+ if ( key && typeof key === 'string' ) {
124
+ addReloadRef . current ?. ( key , reloadArgs =>
125
+ handleRequest . current ( key , reloadArgs ) ,
126
+ )
127
+ }
128
+
129
+ return ( ) => {
130
+ if ( key && typeof key === 'string' ) {
131
+ clearReloadRef . current ?. ( key )
132
+ }
133
+ }
134
+ } , [ enabled , key ] )
135
+
136
+ useLayoutEffect ( ( ) => {
137
+ clearReloadRef . current = clearReload
138
+ addReloadRef . current = addReload
139
+ } , [ clearReload , addReload ] )
140
+
146
141
return {
147
142
data : getCachedData ( key ) || initialData ,
148
143
error,
0 commit comments