@@ -95,6 +95,9 @@ export const useRequest = <D = any, E = Error>(
9595 // We always want to use the most recently-set interval when scheduling the next request.
9696 pollIntervalMsRef . current = pollIntervalMs ;
9797
98+ // Tied to every render and bound to each request.
99+ let isOutdatedRequest = false ;
100+
98101 scheduleRequestRef . current = ( ) => {
99102 // Clear current interval
100103 if ( pollIntervalIdRef . current ) {
@@ -122,6 +125,11 @@ export const useRequest = <D = any, E = Error>(
122125 const response = await sendRequest < D , E > ( httpClient , requestBody ) ;
123126 const { data : serializedResponseData , error : responseError } = response ;
124127
128+ // If an outdated request has resolved, ignore its outdated data.
129+ if ( isOutdatedRequest ) {
130+ return { data : null , error : null } ;
131+ }
132+
125133 setError ( responseError ) ;
126134 // If there's an error, keep the data from the last request in case it's still useful to the user.
127135 if ( ! responseError ) {
@@ -151,8 +159,9 @@ export const useRequest = <D = any, E = Error>(
151159 useEffect ( ( ) => {
152160 scheduleRequestRef . current ! ( ) ;
153161
154- // Clean up timeout.
162+ // Clean up timeout and mark inflight requests as stale if the poll interval changes .
155163 return ( ) => {
164+ isOutdatedRequest = true ;
156165 if ( pollIntervalIdRef . current ) {
157166 clearTimeout ( pollIntervalIdRef . current ) ;
158167 }
0 commit comments