diff --git a/.changeset/happy-banks-repeat.md b/.changeset/happy-banks-repeat.md new file mode 100644 index 00000000000..18d0e1f53bc --- /dev/null +++ b/.changeset/happy-banks-repeat.md @@ -0,0 +1,5 @@ +--- +'@tanstack/query-core': patch +--- + +fix: use Object.is instead of === in replaceEqualDeep to correctly handle NaN equality diff --git a/packages/query-core/src/__tests__/utils.test.tsx b/packages/query-core/src/__tests__/utils.test.tsx index 9cee1b46424..f70069a3631 100644 --- a/packages/query-core/src/__tests__/utils.test.tsx +++ b/packages/query-core/src/__tests__/utils.test.tsx @@ -416,6 +416,22 @@ describe('core/utils', () => { expect(next).toBe(current) }) + + it('preserves reference when objects contain NaN', () => { + const prev = { value: NaN } + const next = { value: NaN } + expect(replaceEqualDeep(prev, next)).toBe(prev) + }) + + it('handles top-level NaN equality', () => { + expect(Number.isNaN(replaceEqualDeep(NaN, NaN))).toBe(true) + }) + + it('preserves reference for arrays containing NaN', () => { + const prev = [NaN, { a: 1 }] + const next = [NaN, { a: 1 }] + expect(replaceEqualDeep(prev, next)).toBe(prev) + }) }) describe('matchMutation', () => { diff --git a/packages/query-core/src/utils.ts b/packages/query-core/src/utils.ts index b29e8ded456..109d02d11f1 100644 --- a/packages/query-core/src/utils.ts +++ b/packages/query-core/src/utils.ts @@ -269,7 +269,7 @@ const hasOwn = Object.prototype.hasOwnProperty */ export function replaceEqualDeep(a: unknown, b: T, depth?: number): T export function replaceEqualDeep(a: any, b: any, depth = 0): any { - if (a === b) { + if (Object.is(a, b)) { return a } @@ -292,7 +292,7 @@ export function replaceEqualDeep(a: any, b: any, depth = 0): any { const aItem = a[key] const bItem = b[key] - if (aItem === bItem) { + if (Object.is(aItem, bItem)) { copy[key] = aItem if (array ? i < aSize : hasOwn.call(a, key)) equalItems++ continue