@@ -60,7 +60,7 @@ export type UseLazyQuery<D extends QueryDefinition<any, any, any, any>> = <R = U
6060
6161export type UseLazyQuerySubscription < D extends QueryDefinition < any , any , any , any > > = (
6262 options ?: SubscriptionOptions
63- ) => [ ( args : QueryArgFrom < D > ) => void , undefined | React . MutableRefObject < QueryActionCreatorResult < any > > [ 'current' ] ] ;
63+ ) => [ ( args : QueryArgFrom < D > ) => void , undefined | QueryActionCreatorResult < any > ] ;
6464
6565export type QueryStateSelector < R , D extends QueryDefinition < any , any , any , any > > = (
6666 state : QueryResultSelectorResult < D > ,
@@ -252,57 +252,62 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
252252 refetchOnFocus,
253253 pollingInterval = 0 ,
254254 } = { } ) => {
255- const forceUpdate = useReducer ( ( ) => ( { } ) , { } ) [ 1 ] ;
256255 const { initiate } = api . endpoints [ name ] as ApiEndpointQuery <
257256 QueryDefinition < any , any , any , any , any > ,
258257 Definitions
259258 > ;
260259 const dispatch = useDispatch < ThunkDispatch < any , any , AnyAction > > ( ) ;
261260
262- const promiseRef = useRef < QueryActionCreatorResult < any > > ( ) ;
263- const lastPromise = promiseRef . current ;
261+ const [ promiseRef , setPromiseRef ] = useState < QueryActionCreatorResult < any > > ( ) ;
262+ const unmountRef = useRef < QueryActionCreatorResult < any > | undefined > ( ) ;
264263
265264 useEffect ( ( ) => {
266265 const subscriptionOptions = {
267266 refetchOnReconnect,
268267 refetchOnFocus,
269268 pollingInterval,
270269 } ;
271- if ( lastPromise && ! shallowEqual ( subscriptionOptions , lastPromise . subscriptionOptions ) ) {
272- lastPromise ?. updateSubscriptionOptions ( subscriptionOptions ) ;
273- lastPromise . subscriptionOptions = subscriptionOptions ;
270+ if ( promiseRef && ! shallowEqual ( subscriptionOptions , promiseRef . subscriptionOptions ) ) {
271+ promiseRef ?. updateSubscriptionOptions ( subscriptionOptions ) ;
272+ setPromiseRef ( ( prev ) => ( { ... prev ! , subscriptionOptions } ) ) ;
274273 }
275- } , [ lastPromise , refetchOnFocus , refetchOnReconnect , pollingInterval ] ) ;
274+ } , [ promiseRef , refetchOnFocus , refetchOnReconnect , pollingInterval ] ) ;
275+
276+ useEffect ( ( ) => {
277+ unmountRef . current = promiseRef ;
278+ } , [ promiseRef ] ) ;
276279
277280 useEffect ( ( ) => {
278281 return ( ) => {
279- promiseRef . current ?. unsubscribe ( ) ;
280- promiseRef . current = undefined ;
282+ unmountRef ?. current ?. unsubscribe ( ) ;
281283 } ;
282284 } , [ ] ) ;
283285
284286 const trigger = useCallback (
285287 function ( arg : any ) {
286- lastPromise ?. unsubscribe ( ) ;
287-
288- const subscriptionOptions = {
289- refetchOnReconnect,
290- refetchOnFocus,
291- pollingInterval,
292- } ;
293-
294- promiseRef . current = dispatch (
295- initiate ( arg , {
296- subscriptionOptions,
297- forceRefetch : true ,
298- } )
299- ) ;
300- forceUpdate ( ) ;
288+ batch ( ( ) => {
289+ promiseRef ?. unsubscribe ( ) ;
290+
291+ const subscriptionOptions = {
292+ refetchOnReconnect,
293+ refetchOnFocus,
294+ pollingInterval,
295+ } ;
296+
297+ const promise = dispatch (
298+ initiate ( arg , {
299+ subscriptionOptions,
300+ forceRefetch : true ,
301+ } )
302+ ) ;
303+
304+ setPromiseRef ( promise ) ;
305+ } ) ;
301306 } ,
302- [ dispatch , initiate , lastPromise , pollingInterval , refetchOnFocus , refetchOnReconnect ]
307+ [ dispatch , initiate , promiseRef , pollingInterval , refetchOnFocus , refetchOnReconnect ]
303308 ) ;
304309
305- return useMemo ( ( ) => [ trigger , lastPromise ] , [ trigger , lastPromise ] ) ;
310+ return useMemo ( ( ) => [ trigger , promiseRef ] , [ trigger , promiseRef ] ) ;
306311 } ;
307312
308313 const useQueryState : UseQueryState < any > = (
0 commit comments