Skip to content

Commit 4a2e838

Browse files
authored
fix(useQueries): make sure we don't lose property tracking (#8295)
if we call `combine` with `#this.result`, we don't have a tracked version of it, as tracking happens in useQueries (to avoid tracking internal access)
1 parent 0df2883 commit 4a2e838

File tree

3 files changed

+94
-13
lines changed

3 files changed

+94
-13
lines changed

packages/query-core/src/queriesObserver.ts

+21-12
Original file line numberDiff line numberDiff line change
@@ -175,21 +175,30 @@ export class QueriesObserver<
175175
return this.#combineResult(r ?? result, combine)
176176
},
177177
() => {
178-
return matches.map((match, index) => {
179-
const observerResult = result[index]!
180-
return !match.defaultedQueryOptions.notifyOnChangeProps
181-
? match.observer.trackResult(observerResult, (accessedProp) => {
182-
// track property on all observers to ensure proper (synchronized) tracking (#7000)
183-
matches.forEach((m) => {
184-
m.observer.trackProp(accessedProp)
185-
})
186-
})
187-
: observerResult
188-
})
178+
return this.#trackResult(result, queries)
189179
},
190180
]
191181
}
192182

183+
#trackResult(
184+
result: Array<QueryObserverResult>,
185+
queries: Array<QueryObserverOptions>,
186+
) {
187+
const matches = this.#findMatchingObservers(queries)
188+
189+
return matches.map((match, index) => {
190+
const observerResult = result[index]!
191+
return !match.defaultedQueryOptions.notifyOnChangeProps
192+
? match.observer.trackResult(observerResult, (accessedProp) => {
193+
// track property on all observers to ensure proper (synchronized) tracking (#7000)
194+
matches.forEach((m) => {
195+
m.observer.trackProp(accessedProp)
196+
})
197+
})
198+
: observerResult
199+
})
200+
}
201+
193202
#combineResult(
194203
input: Array<QueryObserverResult>,
195204
combine: CombineFn<TCombinedResult> | undefined,
@@ -267,7 +276,7 @@ export class QueriesObserver<
267276
if (this.hasListeners()) {
268277
const previousResult = this.#combinedResult
269278
const newResult = this.#combineResult(
270-
this.#result,
279+
this.#trackResult(this.#result, this.#queries),
271280
this.#options?.combine,
272281
)
273282

packages/react-query/src/__tests__/useQueries.test.tsx

+73
Original file line numberDiff line numberDiff line change
@@ -1548,4 +1548,77 @@ describe('useQueries', () => {
15481548
// one with pending, one with foo
15491549
expect(renders).toBe(2)
15501550
})
1551+
1552+
it('should track properties correctly with combine', async () => {
1553+
const key1 = queryKey()
1554+
const key2 = queryKey()
1555+
const key3 = queryKey()
1556+
1557+
const client = new QueryClient()
1558+
1559+
function Page() {
1560+
const data = useQueries(
1561+
{
1562+
queries: [
1563+
{
1564+
queryKey: [key1],
1565+
queryFn: async () => {
1566+
await sleep(10)
1567+
return 'first result'
1568+
},
1569+
},
1570+
{
1571+
queryKey: [key2],
1572+
queryFn: async () => {
1573+
await sleep(15)
1574+
return 'second result'
1575+
},
1576+
},
1577+
{
1578+
queryKey: [key3],
1579+
queryFn: async () => {
1580+
await sleep(20)
1581+
return 'third result'
1582+
},
1583+
},
1584+
],
1585+
combine: (results) => {
1586+
if (results.find((r) => r.isPending)) {
1587+
return 'pending'
1588+
}
1589+
return results.map((r) => r.data).join(', ')
1590+
},
1591+
},
1592+
client,
1593+
)
1594+
1595+
return (
1596+
<div>
1597+
<div>data: {data}</div>
1598+
<button
1599+
onClick={() => {
1600+
client.setQueryData([key1], 'first result updated')
1601+
}}
1602+
>
1603+
update
1604+
</button>
1605+
</div>
1606+
)
1607+
}
1608+
1609+
const rendered = render(<Page />)
1610+
1611+
await waitFor(() => rendered.getByText('data: pending'))
1612+
await waitFor(() =>
1613+
rendered.getByText('data: first result, second result, third result'),
1614+
)
1615+
1616+
fireEvent.click(rendered.getByRole('button', { name: /update/i }))
1617+
1618+
await waitFor(() =>
1619+
rendered.getByText(
1620+
'data: first result updated, second result, third result',
1621+
),
1622+
)
1623+
})
15511624
})

packages/react-query/src/__tests__/useSuspenseQueries.test.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,6 @@ describe('useSuspenseQueries 2', () => {
556556
queryKey: key,
557557
queryFn: async () => {
558558
count++
559-
console.log('queryFn')
560559
throw new Error('Query failed')
561560
},
562561
gcTime: 0,

0 commit comments

Comments
 (0)