@@ -352,6 +352,97 @@ describe('useQueries', () => {
352
352
] )
353
353
} )
354
354
355
+ it ( 'should not go to infinite render loop with previous data when toggling queries' , async ( ) => {
356
+ const key = queryKey ( )
357
+ const states : UseQueryResult [ ] [ ] = [ ]
358
+
359
+ function Page ( ) {
360
+ const [ enableId1 , setEnableId1 ] = React . useState ( true )
361
+ const ids = enableId1 ? [ 1 , 2 ] : [ 2 ]
362
+
363
+ const result = useQueries (
364
+ ids . map ( id => {
365
+ return {
366
+ queryKey : [ key , id ] ,
367
+ queryFn : async ( ) => {
368
+ await sleep ( 5 )
369
+ return id * 5
370
+ } ,
371
+ keepPreviousData : true ,
372
+ }
373
+ } )
374
+ )
375
+
376
+ states . push ( result )
377
+
378
+ React . useEffect ( ( ) => {
379
+ setActTimeout ( ( ) => {
380
+ setEnableId1 ( false )
381
+ } , 20 )
382
+
383
+ setActTimeout ( ( ) => {
384
+ setEnableId1 ( true )
385
+ } , 30 )
386
+ } , [ ] )
387
+
388
+ return null
389
+ }
390
+
391
+ renderWithClient ( queryClient , < Page /> )
392
+
393
+ await waitFor ( ( ) => expect ( states . length ) . toBe ( 9 ) )
394
+
395
+ expect ( states [ 0 ] ) . toMatchObject ( [
396
+ {
397
+ status : 'loading' ,
398
+ data : undefined ,
399
+ isPreviousData : false ,
400
+ isFetching : true ,
401
+ } ,
402
+ {
403
+ status : 'loading' ,
404
+ data : undefined ,
405
+ isPreviousData : false ,
406
+ isFetching : true ,
407
+ } ,
408
+ ] )
409
+ expect ( states [ 1 ] ) . toMatchObject ( [
410
+ { status : 'success' , data : 5 , isPreviousData : false , isFetching : false } ,
411
+ {
412
+ status : 'loading' ,
413
+ data : undefined ,
414
+ isPreviousData : false ,
415
+ isFetching : true ,
416
+ } ,
417
+ ] )
418
+ expect ( states [ 2 ] ) . toMatchObject ( [
419
+ { status : 'success' , data : 5 , isPreviousData : false , isFetching : false } ,
420
+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : false } ,
421
+ ] )
422
+ expect ( states [ 3 ] ) . toMatchObject ( [
423
+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : false } ,
424
+ ] )
425
+ expect ( states [ 4 ] ) . toMatchObject ( [
426
+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : false } ,
427
+ ] )
428
+ expect ( states [ 5 ] ) . toMatchObject ( [
429
+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : false } ,
430
+ { status : 'success' , data : 5 , isPreviousData : false , isFetching : true } ,
431
+ ] )
432
+ expect ( states [ 6 ] ) . toMatchObject ( [
433
+ { status : 'success' , data : 5 , isPreviousData : false , isFetching : true } ,
434
+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : false } ,
435
+ ] )
436
+ expect ( states [ 7 ] ) . toMatchObject ( [
437
+ { status : 'success' , data : 5 , isPreviousData : false , isFetching : true } ,
438
+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : false } ,
439
+ ] )
440
+ expect ( states [ 8 ] ) . toMatchObject ( [
441
+ { status : 'success' , data : 5 , isPreviousData : false , isFetching : false } ,
442
+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : false } ,
443
+ ] )
444
+ } )
445
+
355
446
it ( 'handles type parameter - tuple of tuples' , async ( ) => {
356
447
const key1 = queryKey ( )
357
448
const key2 = queryKey ( )
0 commit comments