Skip to content

Commit

Permalink
Add keepPreviousData
Browse files Browse the repository at this point in the history
  • Loading branch information
christianbaroni committed Jan 5, 2025
1 parent cd8531e commit c024ba6
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/state/internal/createQueryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,12 @@ export type QueryStoreConfig<TQueryFnData, TParams extends Record<string, unknow
* @default true
*/
enabled?: boolean;
/**
* If `true`, the store's `getData` method will always return existing data from the cache if it exists,
* regardless of whether the cached data is expired, until the data is pruned following a successful fetch.
* @default false
*/
keepPreviousData?: boolean;
/**
* The maximum number of times to retry a failed fetch operation.
* @default 3
Expand Down Expand Up @@ -415,6 +421,7 @@ export function createQueryStore<
disableAutoRefetching = false,
disableCache = false,
enabled = true,
keepPreviousData = false,
maxRetries = 3,
onError,
params,
Expand Down Expand Up @@ -744,6 +751,7 @@ export function createQueryStore<
if (disableCache) return null;
const currentQueryKey = params ? getQueryKey(params) : get().queryKey;
const cacheEntry = get().queryCache[currentQueryKey];
if (keepPreviousData) return cacheEntry?.data ?? null;
const isExpired = !!cacheEntry?.lastFetchedAt && Date.now() - cacheEntry.lastFetchedAt > cacheTime;
return isExpired ? null : cacheEntry?.data ?? null;
},
Expand Down

0 comments on commit c024ba6

Please sign in to comment.