File tree Expand file tree Collapse file tree 2 files changed +36
-5
lines changed
Expand file tree Collapse file tree 2 files changed +36
-5
lines changed Original file line number Diff line number Diff line change @@ -464,18 +464,18 @@ export class Query<
464464
465465 const hasInitialData = typeof options . initialData !== 'undefined'
466466
467- const initialDataUpdatedAt =
468- hasInitialData &&
469- ( typeof options . initialDataUpdatedAt === 'function'
467+ const initialDataUpdatedAt = hasInitialData
468+ ? typeof options . initialDataUpdatedAt === 'function'
470469 ? ( options . initialDataUpdatedAt as ( ) => number | undefined ) ( )
471- : options . initialDataUpdatedAt )
470+ : options . initialDataUpdatedAt
471+ : 0
472472
473473 const hasData = typeof data !== 'undefined'
474474
475475 return {
476476 data,
477477 dataUpdateCount : 0 ,
478- dataUpdatedAt : hasData ? initialDataUpdatedAt || Date . now ( ) : 0 ,
478+ dataUpdatedAt : hasData ? initialDataUpdatedAt ?? Date . now ( ) : 0 ,
479479 error : null ,
480480 errorUpdateCount : 0 ,
481481 errorUpdatedAt : 0 ,
Original file line number Diff line number Diff line change @@ -2107,6 +2107,37 @@ describe('useQuery', () => {
21072107 } )
21082108 } )
21092109
2110+ it ( 'should fetch if "initial data updated at" is exactly 0' , async ( ) => {
2111+ const key = queryKey ( )
2112+ const states : UseQueryResult < string > [ ] = [ ]
2113+
2114+ function Page ( ) {
2115+ const state = useQuery ( key , ( ) => 'data' , {
2116+ staleTime : 10 * 1000 , // 10 seconds
2117+ initialData : 'initial' ,
2118+ initialDataUpdatedAt : 0 ,
2119+ } )
2120+ states . push ( state )
2121+ return null
2122+ }
2123+
2124+ renderWithClient ( queryClient , < Page /> )
2125+
2126+ await sleep ( 100 )
2127+
2128+ expect ( states . length ) . toBe ( 2 )
2129+ expect ( states [ 0 ] ) . toMatchObject ( {
2130+ data : 'initial' ,
2131+ isStale : true ,
2132+ isFetching : true ,
2133+ } )
2134+ expect ( states [ 1 ] ) . toMatchObject ( {
2135+ data : 'data' ,
2136+ isStale : false ,
2137+ isFetching : false ,
2138+ } )
2139+ } )
2140+
21102141 it ( 'should keep initial data when the query key changes' , async ( ) => {
21112142 const key = queryKey ( )
21122143 const states : UseQueryResult < { count : number } > [ ] = [ ]
You can’t perform that action at this time.
0 commit comments