Skip to content

Commit fdb0944

Browse files
fix(query-core): replaceEqualDeep correctly handles arrays that contain undefined (#7376)
Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
1 parent 32d1e82 commit fdb0944

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

packages/query-core/src/__tests__/utils.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,20 @@ describe('core/utils', () => {
376376

377377
expect(current).toBe(next)
378378
})
379+
380+
it('should return the previous value when both values are an array of undefined', () => {
381+
const current = [undefined]
382+
const next = replaceEqualDeep(current, [undefined])
383+
384+
expect(next).toBe(current)
385+
})
386+
387+
it('should return the previous value when both values are an array that contains undefined', () => {
388+
const current = [{ foo: 1 }, undefined]
389+
const next = replaceEqualDeep(current, [{ foo: 1 }, undefined])
390+
391+
expect(next).toBe(current)
392+
})
379393
})
380394

381395
describe('matchMutation', () => {

packages/query-core/src/utils.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,9 @@ export function replaceEqualDeep(a: any, b: any): any {
232232
for (let i = 0; i < bSize; i++) {
233233
const key = array ? i : bItems[i]
234234
if (
235-
!array &&
235+
((!array && aItems.includes(key)) || array) &&
236236
a[key] === undefined &&
237-
b[key] === undefined &&
238-
aItems.includes(key)
237+
b[key] === undefined
239238
) {
240239
copy[key] = undefined
241240
equalItems++

0 commit comments

Comments
 (0)