1717 * under the License.
1818 */
1919
20- import { useEffect , useState , useRef } from 'react' ;
20+ import { useEffect , useState , useRef , useMemo } from 'react' ;
2121
2222import { HttpSetup , HttpFetchQuery } from '../../../../../src/core/public' ;
2323
@@ -78,6 +78,7 @@ export const useRequest = <D = any>(
7878 deserializer = ( data : any ) : any => data ,
7979 } : UseRequestConfig
8080) : UseRequestResponse < D > => {
81+ const sendRequestRef = useRef < ( ) => Promise < SendRequestResponse < D > > > ( ) ;
8182 // Main states for tracking request status and data
8283 const [ error , setError ] = useState < null | any > ( null ) ;
8384 const [ isLoading , setIsLoading ] = useState < boolean > ( true ) ;
@@ -102,7 +103,10 @@ export const useRequest = <D = any>(
102103
103104 // Set new interval
104105 if ( pollInterval . current ) {
105- pollIntervalId . current = setTimeout ( _sendRequest , pollInterval . current ) ;
106+ pollIntervalId . current = setTimeout (
107+ ( ) => ( sendRequestRef . current ?? _sendRequest ) ( ) ,
108+ pollInterval . current
109+ ) ;
106110 }
107111 } ;
108112
@@ -145,11 +149,17 @@ export const useRequest = <D = any>(
145149 } ;
146150
147151 useEffect ( ( ) => {
148- _sendRequest ( ) ;
149- // To be functionally correct we'd send a new request if the method, path, or body changes.
152+ sendRequestRef . current = _sendRequest ;
153+ } , [ _sendRequest ] ) ;
154+
155+ const stringifiedQuery = useMemo ( ( ) => JSON . stringify ( query ) , [ query ] ) ;
156+
157+ useEffect ( ( ) => {
158+ ( sendRequestRef . current ?? _sendRequest ) ( ) ;
159+ // To be functionally correct we'd send a new request if the method, path, query or body changes.
150160 // But it doesn't seem likely that the method will change and body is likely to be a new
151- // object even if its shape hasn't changed, so for now we're just watching the path.
152- } , [ path ] ) ;
161+ // object even if its shape hasn't changed, so for now we're just watching the path and the query .
162+ } , [ path , stringifiedQuery ] ) ;
153163
154164 useEffect ( ( ) => {
155165 scheduleRequest ( ) ;
@@ -168,6 +178,6 @@ export const useRequest = <D = any>(
168178 isLoading,
169179 error,
170180 data,
171- sendRequest : _sendRequest , // Gives the user the ability to manually request data
181+ sendRequest : sendRequestRef . current ?? _sendRequest , // Gives the user the ability to manually request data
172182 } ;
173183} ;
0 commit comments