Skip to content

Commit

Permalink
Add disableAutoRefetching
Browse files Browse the repository at this point in the history
  • Loading branch information
christianbaroni committed Jan 4, 2025
1 parent f5af6a3 commit ec8eb14
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/state/internal/createQueryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ export type QueryStoreConfig<TQueryFnData, TParams extends Record<string, unknow
* @default false
*/
debugMode?: boolean;
/**
* If `true`, the store will **not** trigger automatic refetches when data becomes stale. This is
* useful in cases where you want to refetch data on component mount if stale, but not automatically
* if data becomes stale while your component is already mounted.
* @default false
*/
disableAutoRefetching?: boolean;
/**
* Controls whether the store's caching mechanisms are disabled. When disabled, the store will always refetch
* data when params change, and fetched data will not be stored unless a `setData` function is provided.
Expand Down Expand Up @@ -405,6 +412,7 @@ export function createQueryStore<
transform,
cacheTime = time.days(7),
debugMode = false,
disableAutoRefetching = false,
disableCache = false,
enabled = true,
maxRetries = 3,
Expand Down Expand Up @@ -494,8 +502,10 @@ export function createQueryStore<
};

const scheduleNextFetch = (params: TParams, options: FetchOptions | undefined) => {
if (disableAutoRefetching) return;
const effectiveStaleTime = options?.staleTime ?? staleTime;
if (effectiveStaleTime <= 0 || effectiveStaleTime === Infinity) return;

if (activeRefetchTimeout) {
clearTimeout(activeRefetchTimeout);
activeRefetchTimeout = null;
Expand Down Expand Up @@ -824,7 +834,7 @@ export function createQueryStore<

set(state => ({ ...state, queryKey: currentQueryKey }));

if (subscriptionCount === 1 && enabled) {
if ((subscriptionCount === 1 || disableAutoRefetching) && enabled) {
if (isStale() || !get().queryCache[currentQueryKey]?.lastFetchedAt) {
fetch(currentParams);
} else {
Expand Down

0 comments on commit ec8eb14

Please sign in to comment.