Skip to content

Commit 3025dc7

Browse files
author
ismay
committed
fix(use-data-query): use ref to store variables hash
1 parent e96d984 commit 3025dc7

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

services/data/src/react/hooks/useDataQuery.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const useDataQuery = (
3030
lazy: initialLazy = false,
3131
}: QueryOptions = {}
3232
): QueryRenderInput => {
33+
const variablesHash = useRef<string | null>(null)
3334
const [variables, setVariables] = useState(initialVariables)
3435
const [enabled, setEnabled] = useState(!initialLazy)
3536
const [staticQuery] = useStaticInput<Query>(query, {
@@ -104,9 +105,13 @@ export const useDataQuery = (
104105
}
105106

106107
if (newVariables) {
107-
const merged = { ...variables, ...newVariables }
108-
const identical =
109-
stableValueHash(variables) === stableValueHash(merged)
108+
// Use cached hash if it exists
109+
const currentHash =
110+
variablesHash.current || stableValueHash(variables)
111+
112+
const mergedVariables = { ...variables, ...newVariables }
113+
const mergedHash = stableValueHash(mergedVariables)
114+
const identical = currentHash === mergedHash
110115

111116
if (identical) {
112117
// If the variables are identical we'll need to trigger the refetch manually
@@ -115,7 +120,8 @@ export const useDataQuery = (
115120
throwOnError: false,
116121
}).then(({ data }) => data)
117122
} else {
118-
setVariables(merged)
123+
variablesHash.current = mergedHash
124+
setVariables(mergedVariables)
119125
}
120126
}
121127

0 commit comments

Comments
 (0)