File tree Expand file tree Collapse file tree 2 files changed +59
-6
lines changed
Expand file tree Collapse file tree 2 files changed +59
-6
lines changed Original file line number Diff line number Diff line change @@ -666,8 +666,8 @@ function shouldFetchOptionally(
666666 prevOptions : QueryObserverOptions < any , any >
667667) : boolean {
668668 return (
669- ( query !== prevQuery ||
670- ( options . enabled !== false && prevOptions . enabled === false ) ) &&
669+ options . enabled !== false &&
670+ ( query !== prevQuery || prevOptions . enabled === false ) &&
671671 isStale ( query , options )
672672 )
673673}
Original file line number Diff line number Diff line change @@ -1218,6 +1218,59 @@ describe('useQuery', () => {
12181218 } )
12191219 } )
12201220
1221+ it ( 'should not fetch when switching to a disabled query' , async ( ) => {
1222+ const key = queryKey ( )
1223+ const states : UseQueryResult < number > [ ] = [ ]
1224+
1225+ function Page ( ) {
1226+ const [ count , setCount ] = React . useState ( 0 )
1227+
1228+ const state = useQuery (
1229+ [ key , count ] ,
1230+ async ( ) => {
1231+ await sleep ( 5 )
1232+ return count
1233+ } ,
1234+ { enabled : count === 0 }
1235+ )
1236+
1237+ states . push ( state )
1238+
1239+ React . useEffect ( ( ) => {
1240+ setActTimeout ( ( ) => {
1241+ setCount ( 1 )
1242+ } , 10 )
1243+ } , [ ] )
1244+
1245+ return null
1246+ }
1247+
1248+ renderWithClient ( queryClient , < Page /> )
1249+
1250+ await sleep ( 50 )
1251+
1252+ expect ( states . length ) . toBe ( 3 )
1253+
1254+ // Fetch query
1255+ expect ( states [ 0 ] ) . toMatchObject ( {
1256+ data : undefined ,
1257+ isFetching : true ,
1258+ isSuccess : false ,
1259+ } )
1260+ // Fetched query
1261+ expect ( states [ 1 ] ) . toMatchObject ( {
1262+ data : 0 ,
1263+ isFetching : false ,
1264+ isSuccess : true ,
1265+ } )
1266+ // Switch to disabled query
1267+ expect ( states [ 2 ] ) . toMatchObject ( {
1268+ data : undefined ,
1269+ isFetching : false ,
1270+ isSuccess : false ,
1271+ } )
1272+ } )
1273+
12211274 it ( 'should keep the previous data when keepPreviousData is set' , async ( ) => {
12221275 const key = queryKey ( )
12231276 const states : UseQueryResult < number > [ ] = [ ]
@@ -1525,7 +1578,7 @@ describe('useQuery', () => {
15251578 // Set state
15261579 expect ( states [ 3 ] ) . toMatchObject ( {
15271580 data : 0 ,
1528- isFetching : true ,
1581+ isFetching : false ,
15291582 isSuccess : true ,
15301583 isPreviousData : true ,
15311584 } )
@@ -1600,14 +1653,14 @@ describe('useQuery', () => {
16001653 // Set state
16011654 expect ( states [ 1 ] ) . toMatchObject ( {
16021655 data : 10 ,
1603- isFetching : true ,
1656+ isFetching : false ,
16041657 isSuccess : true ,
16051658 isPreviousData : true ,
16061659 } )
1607- // Set state
1660+ // State update
16081661 expect ( states [ 2 ] ) . toMatchObject ( {
16091662 data : 10 ,
1610- isFetching : true ,
1663+ isFetching : false ,
16111664 isSuccess : true ,
16121665 isPreviousData : true ,
16131666 } )
You can’t perform that action at this time.
0 commit comments