From a6f899804b8f2c78b96324524593cc2ef23b37be Mon Sep 17 00:00:00 2001 From: Christian Baroni <7061887+christianbaroni@users.noreply.github.com> Date: Mon, 30 Dec 2024 21:40:51 +0000 Subject: [PATCH] Ensure effectiveParams and currentQueryKey are always in sync within each fetch operation --- src/state/internal/createQueryStore.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/state/internal/createQueryStore.ts b/src/state/internal/createQueryStore.ts index 491e8a81f58..0418c1a5029 100644 --- a/src/state/internal/createQueryStore.ts +++ b/src/state/internal/createQueryStore.ts @@ -482,7 +482,7 @@ export function createQueryStore< clearTimeout(activeRefetchTimeout); activeRefetchTimeout = null; } - const currentQueryKey = get().queryKey; + const currentQueryKey = getQueryKey(params); const lastFetchedAt = get().queryCache[currentQueryKey]?.lastFetchedAt || (disableCache && lastFetchKey === currentQueryKey ? get().lastFetchedAt : null); const timeUntilRefetch = lastFetchedAt ? effectiveStaleTime - (Date.now() - lastFetchedAt) : effectiveStaleTime; @@ -500,8 +500,8 @@ export function createQueryStore< if (!options?.force && !get().enabled) return; const effectiveParams = params ?? getCurrentResolvedParams(); - const { queryKey: currentQueryKey, status } = get(); - const isLoading = status === QueryStatuses.Loading; + const currentQueryKey = getQueryKey(effectiveParams); + const isLoading = get().status === QueryStatuses.Loading; if (activeFetchPromise && lastFetchKey === currentQueryKey && isLoading && !options?.force) { return activeFetchPromise; @@ -733,12 +733,13 @@ export function createQueryStore< } }); - const { enabled, fetch, isStale, queryKey } = get(); - + const { enabled, fetch, isStale } = get(); const currentParams = getCurrentResolvedParams(); - set(state => ({ ...state, queryKey: getQueryKey(currentParams) })); + const currentQueryKey = getQueryKey(currentParams); + + set(state => ({ ...state, queryKey: currentQueryKey })); - if (!get().queryCache[queryKey] || isStale()) { + if (!get().queryCache[currentQueryKey] || isStale()) { fetch(currentParams); } else if (enabled) { scheduleNextFetch(currentParams, undefined);